简体   繁体   English

字符编码问题

[英]Character encoding problem

Greetings, 问候,

I'm developing a project in C++ where I want to use characters like á é õ and ┌ ─ ┐ │ to draw a couple of nice frames. 我正在用C ++开发一个项目,我想用á é õ┌ ─ ┐ │等字符来绘制几个漂亮的帧。 My doubt resides in what I should change in my code/project settings since, without any kind of modifications, the console just prints pseudo-random characters. 我的疑问在于我应该在我的代码/项目设置中进行更改,因为在没有任何修改的情况下,控制台只会打印伪随机字符。

I know that the above characters are defined in the character set Code page 437 aka Extended ASCII, but what I should do know? 我知道上面的字符是在字符集代码页437又名扩展ASCII中定义的,但我应该知道什么?

After some research, I included the instruction setlocale(LC_CTYPE, ""); 经过一些研究,我加入了指令setlocale(LC_CTYPE, ""); and now I can print accented characters (à é õ) but can't print the borders. 现在我可以打印重音字符(àéõ)但不能打印边框。

Also, should I use char and string or wchar and wstring to use these characters? 另外,我应该使用charstringwcharwstring来使用这些字符吗?

Thanks 谢谢

I think the best way to do it would be to use wchar and wstring for the characters - they are meant for locale-independant string operations and are defined as UTF-16 in Windows and as UTF-32 in Linux. 我认为最好的方法是使用wchar和wstring作为字符 - 它们用于与语言环境无关的字符串操作,在Windows中定义为UTF-16,在Linux中定义为UTF-32。

Note that you need to use the proper functions, for example wprintf instead of printf ... If you're using iostream , I think that should work out-of-the-box with wstrings. 请注意,您需要使用正确的函数,例如wprintf而不是printf ...如果您正在使用iostream ,我认为它应该与wstrings开箱即用。

EDIT: Note that it is not required for wchar_t to be unicode (in practice, it often is). 编辑:请注意, wchar_t不需要unicode(实际上,它通常是)。 If wchar_t (and thus, wstring ) is unicode, then the C99 standard (and therefore most likely the C++ standard) states that __STDC_ISO_10646__ is to be defined. 如果wchar_t (因此, wstring )是unicode,那么C99标准(因此很可能是C ++标准)声明要定义__STDC_ISO_10646__

In other words, if __STDC_ISO_10646__ is defined, then the wchar_t is unicode -- as for the exact type (UTF-16 or UTF-32), you can use a sizeof(wchar_t). 换句话说,如果定义了__STDC_ISO_10646__ ,则wchar_t是unicode - 对于确切类型(UTF-16或UTF-32),您可以使用sizeof(wchar_t)。

尝试使用wchar_t

To see which Unicode characters to use look up http://unicode.org/charts/ and search for "Box Drawing". 要查看要使用的Unicode字符,请查找http://unicode.org/charts/并搜索“Box Drawing”。 That set includes the characters that are in CP 437, but also many others. 该集包括CP 437中的字符,但也包括许多其他字符。 Next question is whether your device can show some or all of these. 接下来的问题是您的设备是否可以显示其中的部分或全部内容。

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

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