943,891 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 653
  • C++ RSS
Nov 29th, 2008
0

Chars becoming negative.

Expand Post »
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:

cpp Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Nov 29th, 2008
0

Re: Chars becoming negative.

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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Nov 29th, 2008
0

Re: Chars becoming negative.

Click to Expand / Collapse  Quote originally posted by dougy83 ...
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:
C++ Syntax (Toggle Plain Text)
  1. int ich;
  2. ...
  3. ich = (str[i]&0xFF); // absolutely portable code
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 29th, 2008
0

Re: Chars becoming negative.

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!
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Programmers please help....
Next Thread in C++ Forum Timeline: Inheritance and STL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC