#### Archived

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

# Typecast problem, strange

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

## Recommended Posts

I''ve got a weird little problem while trying to convert a string of characters into their numbers in the ascii table, eg ''A'' = 65 and so on. The problem is, when I come over ascii number 127 it starts acting weird. The character ''Ç'' should convert into 128. char c = ''Ç''; int i = (int)c; // Then write it. I get i = -57 from this. I know this is probably something silly, it usually is Thanks for any replies/ -Lord Maz-

##### Share on other sites
You're really declaring a signed char in your code snippet since most compilers default to signed chars when no subtype is specified:

char c = 'Ç'; // really signed char c = 'Ç'

The problem is that a char can only hold 256 values, and as a result, a signed char supports the numbers -128 to +127. At 128, the variable overflows and you get an invalid number. Try declaring c as an unsigned char to get a range of 0 to 255.

RapscallionGL - arriving soon.

[edited by - johnnie2 on December 1, 2002 3:00:56 PM]

##### Share on other sites
Okay thanks, that explains the problem, but I'm still having trouble solving it.

Hard to explain the details briefly, but I don't want to rewrite my code from the beginning. I've got a string of (signed) chars that the user inputs, basicly. Is there any simple way to translate these characters to what they should look like when they are unsigned? Such as 'Ç', which is -57 signed; should be translated to 128.

If this is impossible (and I have to rewrite my code), is there any library functions available for unsigned char strings, such as strlen, strcpy and so on? I don't want to reinvent the wheel for this little project im working with..

-Lord Maz-

[edited by - Lord Maz on December 1, 2002 5:20:17 PM]

##### Share on other sites
the one, and hopefully only, key here is unsigned!

char c = ''Ç'';
unsigned int i = (unsigned int)c;

printf("The ASCHII code is %i, or in hex 0x%02x", i, i);

##### Share on other sites
Why do you need to?
Whats wrong with storing them as signed chars, and converting them to unsigned when you want to do the i=ch bit:
int i = (int)(unsigned char)c;

##### Share on other sites
That is fine too, and probably better as well. Btw, char/string arrays should always be of type unsigned char anyway, so no sign casting should be necessary.

##### Share on other sites
Thanks for all the replies, but I still can''t solve this :/

Okay, some details. I''ve made a text-inputer, works like a chatbox in the usual games (you write some text, press enter and it is sent away to your pal). When i tested it by outputting the text in a messagebox, it looked like it should, all the extended ascii chars too.

Now I want to use it in a OpenGL test program I''ve just started at, so I went the way as descripbed in NeHe tutorial # 17. For those who doesnt know it, it uses the ascii value of the character to select the correct character from a bitmap, so if I were to write "Ç" (for some reason ), it should be character number 128. But it isn''t, and I can''t find a simple way to convert it.

I''ve tried the ways you suggested, but I got either the same value or something even weirder each time..

-Lord Maz-

##### Share on other sites
Ok, this has got me going now. what on earth does the number -57 have to do with 128? MSVC translates ''Ç'' to be -57. AFAIK this means nothing, not sign bitted, not 2''s complement :s

128 in binary: 10000000
-57 = 0xc7 = 11000111

Ive been looking at this for 20 mins, and i can;t make any sense of it.

##### Share on other sites
Are you sure that ''Ç'' shoudl be 128? I wrote a quick proggy to print all the characters from 32 to 256, and it produced this:

!"#\$%&''()*+,-./012345
6789:;<=>?@ABCDEFGHIJK
LMNOPQRSTUVWXYZ[\]^_`a
bcdefghijklmnopqrstuvw
xyz{|}~€‚ƒ„…†‡ˆ‰Š‹Œ
Ž‘’“”•–—˜™š›œžŸ ¡¢£
»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐ
ÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæ
çèéêëìíîïðñòóôõö÷øùúûü
ýþÿ

Unless of course MSVC is using a different character set or soemthing

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 16
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632928
• Total Posts
3009268
• ### Who's Online (See full list)

There are no registered users currently online

×