how to print non-ASCII characters in C

回眸只為那壹抹淺笑 提交于 2019-12-11 10:56:56

问题


I try to output the list of characters with C: http://www.alt-codes.net/

for (i=0; i<len; i++){
    printf("%d\t: %c", i, i);
}

The problem that for all non-ASCII chars I got ? working on Ubuntu.

How can I output them in nice manner.


回答1:


The formatting will be poor, but apart from that the code you posted works.

ASCII characters 0-31 are various space characters and there is no standardized way to print them. The link you posted is a common, yet non-standard "extended ASCII table". There is no guarantee that those exact symbols will be printed on your particular platform.

They work fine for me in Windows 7, tested with GCC and Embarcadero C++, both print those symbols. But on another OS and/or compiler, different symbols or nothing at all might be printed.

Only ASCII characters 32 - 126 are guaranteed to be printable, and the same symbol, on all systems.




回答2:


Various ASCII characters, eg many of those below 32, are not printable. You need to translate the unprintable ones into something else (eg a space) before printing.

Note that the ALT-codes you linked to are not ASCII codes.




回答3:


Your charset may not contain characters such as . What you are looking at is a list of alt key codes symbols, not ASCII at all.




回答4:


Depending on your needs, you can take the approach of an hexadecimal editor and print only those characters that have a graphical representation, for that purpose you can use isprint() or isgraph() (the latest is essentially the same as isprint() but don't print the space) defined in ctype.h, for example:

for (i = 0; i < len; i++) {
    printf("%d\t: %c", i, isprint(i) ? i : '.');
}


来源:https://stackoverflow.com/questions/13050899/how-to-print-non-ascii-characters-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!