如何打印一个汉字? [英] How to print a Chinese character?

查看:45
本文介绍了如何打印一个汉字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C 中打印中文或日文字符(非英文)?

How do I print a Chinese or Japanese character (non-English) in C?

wchar *wc = (wchar *)calloc(50,sizeof(wchar*));
wcscpy(wc ,L"john 麥克風");
printf(" wc : %S \n",wc);

我的输出中只有 john.

推荐答案

软件不知道字符".它所知道的只是恰好被其他软件实际显示输出的解释为字符的数字.(对于您的软件,a 只是 0x61 的别名.将 0x61 转换为识别为 a 的像素组合取决于您的终端、GUI 或其他任何东西.)

Software doesn't know about "characters". All it knows are numbers which happen to be interpreted as characters by whatever other software actually displays your output. (For your software, a is merely an alias for 0x61. Turning that 0x61 into the combination of pixels recognized as a is up to your terminal, your GUI or whatever.)

  1. 源代码中非 ASCII 字符的处理是实现定义的,即由您的编译器来处理源代码中的中文字符.如果你想安全(和便携)玩它,你将不得不使用它们的编码值编写非 ASCII 字符,例如通过使用 \x 符号.(或较新的 \u,但这并没有像它已经明确地引用 Unicode 那样巧妙地说明问题.)

  1. Handling of non-ASCII characters in your source is implementation-defined, i.e. it's up to your compiler what to do with the Chinese characters in your source. If you want to play it safe (and portable), you will have to write non-ASCII characters using their encoding values, e.g. by using \x notation. (Or the newer \u, but that doesn't make the point as neatly as it already refers to Unicode explicitly.)

此时您应该意识到您必须同意您的程序关于您使用的什么编码,否则您的数值将意味着与您的程序完全不同的东西.这是通过适当设置语言环境来完成的.(有编码 ISO-8859-1 和 ISO-8859-15 和 EBCDIC 和 ... 用于单字节,UTF-8 和 UTF-16 和 EUC 和 ISO-2022-JP 和 ... 用于多字节,UCS-2 和 UTF-32 以及其他可能的广泛的......虽然一切应该都是 Unicode 恕我直言,它显然不是,即使没有异国情调,那里也有三种非常不同的 Unicode 编码.)

It should occur to you at this point that you have to agree with your program about what encoding you are using, or your numerical values would mean something entirely different to your program. This is done by setting the locale appropriately. (There are the encodings ISO-8859-1 and ISO-8859-15 and EBCDIC and ... for single-byte, UTF-8 and UTF-16 and EUC and ISO-2022-JP and ... for multi-byte, UCS-2 and UTF-32 and probably others for wide... while everything should be Unicode IMHO, it clearly isn't, and even without the exotics there are three very different Unicode encodings out there.)

正如其他人已经指出的那样,如果您要进行宽"输出,则应使用宽"输出函数.(请注意,多字节"完全是另一种类型的鱼,并且有 8 位多字节编码 -- UTF-8 -- 以及 16 位多字节编码 -- UTF16...)

As others already pointed out, if you're doing "wide" output, you should use "wide" output functions. (Be aware that "multibyte" is yet another type of fish entirely, and that there are 8-bit multibyte encodings -- UTF-8 -- as well as 16-bit multibyte encodings -- UTF16...)

无论您要打印输出(例如终端、GUI)都需要支持您的区域设置/编码,并且需要在其上使用字体处理实际上具有为相关代码点加载的字形(即可打印字符).(否则你会得到?",一些其他占位符,或者一些看起来很有趣的东西,里面有很小的十六进制代码.)

Whatever you are printing your output to (e.g. the terminal, GUI) needs to support your locale / encoding, and needs to have a font at its disposal that actually has glyphs (i.e. printable characters) loaded for the associated code points. (Otherwise you will get "?", some other placeholder, or some funny looking stuff with tiny hexcodes in it.)

虽然您的实际问题很可能在于使用 printf() 而不是 wprintf(),但上述所有要点必须协同工作才能正确打印非 ASCII人物.因此,您可能还有其他问题,但很难说.

While your actual problem most likely rests with using printf() instead of wprintf(), all the above points must work together in order to correctly print non-ASCII characters. Hence, you might have other problems as well, but it's hard to tell.

这篇关于如何打印一个汉字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆