Chars becoming negative.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 950
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is online now Online
Posting Shark

Chars becoming negative.

 
0
  #1
Nov 29th, 2008
Here's what I'm trying out:

Substitute characters into values with no set left bits;
check if the next position is a vowel(or whatever), turn it into a
value with no right bits set, join the two characters
into a single byte, delete the unused position now.

Here's the code using it, that I'm trying to get to work:

  1. for(int i = 0; i < str.length(); i++)
  2. {
  3. /*swaps common letters with stuff in the 32-47 range*/
  4. str[i] = Subcipher(str[i]);
  5.  
  6. if(str[i] < 127)
  7. {
  8. /*attempt to bring that common swapped stuff, below 16*/
  9. str[i] -= 32;
  10.  
  11. /*check are bounadries!*/
  12. if(str.length() - 1 != i)
  13. {
  14. /*if less/equal than 00001111*/
  15. if(str[i] < 16)
  16. {
  17. /*is it in the alphabet range?*/
  18. if(str[i + 1] > 96 && str[i + 1] < 118)
  19. {
  20. temp = str[i + 1];
  21.  
  22. if(temp == 'a') { temp = 128; join = true; }
  23. /*...replacing vowels and stuff with no right set bits...*/
  24. else if(temp == 'u') { temp = 240; join = true; }
  25.  
  26. if(join)
  27. {
  28. str[i] |= temp;
  29. str.erase(i + 1, 1);
  30. join = false;
  31. } } } } } }
The problem is that when I attempt to "join" the values, it becomes negative/less than 128, even when I use something like str[i] +=temp; , instead.

Can anyone point out if I'm just being stupid?

BTW, this is a compression algorithm that places vowels into a single byte, using the extended ASCII values to shove them on-top of. The reverse should be simpler, just having to shift stuff back-and-forth by four.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Chars becoming negative.

 
0
  #2
Nov 29th, 2008
assuming str is of type 'string', str[i] is of type 'char'. chars will be considered negative whenever the top bit is set. It's not an issue if you are bitwise ORing, as you will get the correct result.

If it annoys you, feel free to cast to unsigned char, as necessary.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Chars becoming negative.

 
0
  #3
Nov 29th, 2008
Originally Posted by dougy83 View Post
assuming str is of type 'string', str[i] is of type 'char'. chars will be considered negative whenever the top bit is set. It's not an issue if you are bitwise ORing, as you will get the correct result.

If it annoys you, feel free to cast to unsigned char, as necessary.
Inaccurate statement. The char type may be signed or unsigned, it's an implementation-dependent issue. All chars with true predicate c > '\0' && c <= '\x7f' (ascii-chars) are converted to positive integers, others may be positive or negative (or zero).
So before any manipulations with char bits assign char value to int or unsigned int to suppress possible left bit sign effect, for example:
  1. int ich;
  2. ...
  3. ich = (str[i]&0xFF); // absolutely portable code
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 950
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is online now Online
Posting Shark

Re: Chars becoming negative.

 
0
  #4
Nov 29th, 2008
I see what I was doing now, thinking backwards again and confusing the ranges of unsigned and signed.
It's working now, all I have to do is fix up my substitution cipher and I have my first working compression algorithm - I feel happy
Thanks!
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC