Re: Force 'char' to be implemented as 'unsigned char'?
Matthias Kluwe <mkluwe@gmail.com> wrote:
I came across a coding standard manual (http://www.codingstandard.com/
HICPPCM/index.html) and decided to make up my mind about the suggestions
in there.
I quickly found that I don't understand the justifications for many of
the given rules very well. Here's an example (item 2.2):
"Specify in your compiler configuration that plain 'char' is implemented
as 'unsigned char'. Justification: Support 8-bit ASCII for
internationalisation. The size and sign of char is implementation-
defined. If the range of type char corresponds to 7-bit ASCII, and 8-bit
characters are used, unpredictable behaviour may result. Otherwise prefer
to use wchar_t type."
Hmm, I don't feel very well forcing my compiler in an area where the
language does not enforce. Second, I don't see why an '8-bit character'
having a 'negative value' (encoded as a char) could ever be harmful. Do
you know any examples?
Since I have to deal with this on a regular basis, I'll give you an
example...
On the Nintendo DS and Nintendo Wii, I have an array of images, each one
corresponding to a particular letter. Displaying a word amounts to
drawing the correct images in the correct order...
// its a bit more complex than the below of course, but this gets
// the point across.
void display( char* c, int x, int y ) {
while ( *c ) {
image[*c].draw( x, y );
y += image[*c].width();
++c;
}
}
Do you see the problem with the above if a char is signed and someone
tries to draw, "Ren?"? (thats 0x52, 0x65, 0x6E, 0xE9.)
Also, converting from a char to a wchar_t leads to surprising (and
incorrect) results sometimes:
char c = 0xE9; '?'
wchar_t wc = c;
assert( wc == c ); // this will succeed
assert( wc == 0xE9 ); // this will fail
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]