简体   繁体   English

C:错误的char到ascii转换(扩展的ascii表中的char)

[英]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): 我从像这样的一个字符(使用C语言)中获取ascii代码:

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. 它适用于ASCII表(0至127的字符数),但不适用于扩展ASCII表中的字符。 For example I get -62 from this symbol "°" (it should be 248). 例如,我从该符号“°”得到-62(应该为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 :/ 我尝试了几种编码,例如UTF-8和ISO 8859-1(我使用的是Eclipse cdt btw),每次都得到一个不同的ASCII代码,但不是一个好的代码:/

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. 因为char是从-127到127。您不能使用扩展的ASCII表。

I suggest you use wchar instead. 我建议您改用wchar Check wchar.h and wctype.h manual. 检查wchar.hwctype.h手册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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