Search code examples
cencodingutf-8asciieclipse-cdt

C : wrong char to ascii conversion (char from the extended ascii table)


I get the ascii code from a char like this (using C langage):

char * text = "5°"; 
int sizeText = strlen(text);
int i = 0;
float asciiCode;
for(i=0; i<sizeText; i++) {
    asciiCode = (GLfloat)text[i];
}

It's working well for the ASCII table (chars number 0 to 127) but it's not working with characters from the extended ASCII table. For example I get -62 from this symbol "°" (it should be 248).

I tried with several encoding like UTF-8 and ISO 8859-1 (I'm using eclipse cdt btw), I got a different ASCII code each time but not the good one :/

Do you have any idea why it is not working? How can I get it work?

Thanks.


Solution

  • Because char is from -127 to 127. You can't use extended ASCII table like that.

    I suggest you use wchar instead. Check wchar.h and wctype.h manual.