简体   繁体   中英

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.

我认为您的问题是assiiCode是浮点型,请尝试将其声明为unsigned int

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM