Archived

This topic is now archived and is closed to further replies.

Unsigned char and char?

This topic is 5779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Whats the difference between the two? And could it be the cause of a problem I have with storing characters in a text buffer (with unsigned char, the string can never go above 22 chars in length).

Share this post


Link to post
Share on other sites
Both are 8-bit data, so that''s a no to your question. ASCII characters are in the range 0 to 126 anyways.

The difference is that unsigned char ranges from 0 to 255, while signed char ranges from -128 to 127. Unqualified ''char'' may stand for one or the other depending on your compiler.

Share this post


Link to post
Share on other sites
quote:
Original post by Useless Hacker
Why on earth would you need a signed char?


Anytime you needed a signed byte. In C and C++, a char is always one byte.

--
The Dilbert Principle: People are idiots.

Share this post


Link to post
Share on other sites
quote:
Original post by superpeter
unicode use 2 byte for every character.


Correct, but unicode can't be held within a C++ "char". That's what a "wide char", wchar_t, is for.

quote:
Useless Hacker
Why not just use a byte then. Use chars for chars ('a', 'b') and bytes for numbers (1, 2, 3). Thats what I always do.



And how, in C or C++ code, do you propose to specify you want to "use a byte", unless it's by using char? A char *is a* byte.

--
The Dilbert Principle: People are idiots.


Edited by - SabreMan on February 18, 2002 9:10:14 AM

Share this post


Link to post
Share on other sites
quote:
Original post by Useless Hacker
Why not just use a byte then. Use chars for chars (''a'', ''b'') and bytes for numbers (1, 2, 3). Thats what I always do.


Windef.h, line 143
typedef unsigned char BYTE;

Share this post


Link to post
Share on other sites
(unsigned char)255 is equal to (unsigned char)-1

Chars will sort of "wrap around". I dont really see any merit in declaring an unsigned char because it finally makes no difference anyway, or at least i dont know of any.

Of note: ''A'' is the equal to (char)65.

Share this post


Link to post
Share on other sites
quote:
Original post by Promit
(unsigned char)255 is equal to (unsigned char)-1

Chars will sort of "wrap around". I dont really see any merit in declaring an unsigned char because it finally makes no difference anyway, or at least i dont know of any.


What about when the data you want to hold in an 8-bit char requires a range of 0-255?

quote:

Of note: ''A'' is the equal to (char)65.

...on ASCII systems.



--
The Dilbert Principle: People are idiots.

Share this post


Link to post
Share on other sites
YES, there''s a HUGE difference. I recently ran through some parsing code for MIDI files, and had this logic:
  
void parse (const char *rawData, int dataLen)
{
//...

switch (*rawData & 0xF0) // switch on the status type

{
case 0x80: func1 (); break;
case 0x90: func2 (); break;
// etc.

default: throw runtime_error ("No status byte!");
}
}

Guess what? None of those switches ever worked, it threw every time even when I was specifically looking at *rawData and it was equal to 0x92 or something. In order for this to work, the switch statement has to be:
switch ((unsigned char) (*rawData & 0xF0))

Strings are signed characters by default in MSVC, and I believe it''s left up to the implementation to decide what types their strings are. In other words, if you try to do this in MSVC:
unsigned char *str = "Hello world";
..you''ll get a conversion error. So you can''t "just use unsigned everywhere", because some things are singed by default.

The main difference between signed and unsigned comes with bitshifting. If you bitshift right on a char and the high bit is set, it will sign-extend if it''s a signed character, and will not if it''s unsigned.

So yes, there''s a huge difference between signed and unsigned characters.

Share this post


Link to post
Share on other sites