在C语言中打印宽字符出现问题。

huangapple go评论97阅读模式
英文:

Problem with printing wide character in c

问题

你好,我想打印字母'å',它在扩展ASCII中是131,据我所知,它的UTF-8代码是00E5,UTF-16中是0x00E5。然而,在下面的代码中,程序打印出了'Õ',这不是我想要的。我不知道我做错了什么。提前谢谢。

  1. int main(void) {
  2. wchar_t myChar = 0x00E5;
  3. wprintf(L"%lc", myChar);
  4. }
英文:

Hello I want to print the letter 'å' which is 131 in extended ascii and as far as I can see has UTF-8 code 00E5 and 0x00E5 in UTF-16. However, in the code below the program prints 'Õ' which is not what I wanted. I do not know what I have done wrong. Thanks in advance.

  1. int main(void) {
  2. wchar_t myChar = 0x00E5;
  3. wprintf(L"%lc", myChar);
  4. }

答案1

得分: 4

我会尝试使用UTF-16字符字面量,并设置区域设置(setlocale)为CP_UTF_16LE(如果使用MSVC)或用户首选区域设置(如果不是)。

  1. #ifdef _MSC_VER
  2. #include <io.h> // _setmode
  3. #include <fcntl.h> // _O_U16TEXT
  4. #endif
  5. #include <stdio.h>
  6. #include <locale.h>
  7. #include <wchar_t.h>
  8. void set_locale_mode() {
  9. #ifdef _MSC_VER
  10. // Unicode UTF-16,小端字节顺序(ISO 10646的BMP)
  11. const char *CP_UTF_16LE = ".1200";
  12. setlocale(LC_ALL, CP_UTF_16LE);
  13. _setmode(_fileno(stdout), _O_U16TEXT);
  14. #else
  15. // 设置用户首选区域设置
  16. setlocale(LC_ALL, "");
  17. #endif
  18. }
  19. int main(void) {
  20. set_locale_mode(); // 在任何其他输出之前执行此操作
  21. // 使用Unicode为`&#229;`的UTF-16字符字面量:
  22. wchar_t myChar = u'\u00E5';
  23. wprintf(L"%lc", myChar);
  24. }

在Windows 11上使用VS2022进行了测试。

英文:

I'd try using an UTF-16 character literal and also set the locale (setlocale) to CP_UTF_16LE if using MSVC or to the user-preferred locale if not.

  1. #ifdef _MSC_VER
  2. #include &lt;io.h&gt; // _setmode
  3. #include &lt;fcntl.h&gt; // _O_U16TEXT
  4. #endif
  5. #include &lt;stdio.h&gt;
  6. #include &lt;locale.h&gt;
  7. #include &lt;wchar.h&gt;
  8. void set_locale_mode() {
  9. #ifdef _MSC_VER
  10. // Unicode UTF-16, little endian byte order (BMP of ISO 10646)
  11. const char *CP_UTF_16LE = &quot;.1200&quot;;
  12. setlocale(LC_ALL, CP_UTF_16LE);
  13. _setmode(_fileno(stdout), _O_U16TEXT);
  14. #else
  15. // set the user-preferred locale
  16. setlocale(LC_ALL, &quot;&quot;);
  17. #endif
  18. }
  19. int main(void) {
  20. set_locale_mode(); // do this before any other output
  21. // UTF-16 character literal with unicode for `&#229;`:
  22. wchar_t myChar = u&#39;\u00E5&#39;;
  23. wprintf(L&quot;%lc&quot;, myChar);
  24. }

Tested in Windows 11 with VS2022

答案2

得分: 1

在Windows上,要输出Unicode字符,需要更改控制台模式:

  1. #include&lt;fcntl.h&gt;
  2. #include &lt;io.h&gt;
  3. #include &lt;stdio.h&gt;
  4. int main(void) {
  5. _setmode(_fileno(stdout), _O_U16TEXT);
  6. // UTF-16 character literal with unicode for `&#229;`:
  7. wchar_t myChar = 0x00E5;
  8. // Adding U+20AC EURO SIGN as well.
  9. wprintf(L"\u20ac%lc", myChar);
  10. }

输出:

  1. ی
英文:

On Windows, to output Unicode characters the console mode needs to be changed:

  1. #include&lt;fcntl.h&gt;
  2. #include &lt;io.h&gt;
  3. #include &lt;stdio.h&gt;
  4. int main(void) {
  5. _setmode(_fileno(stdout), _O_U16TEXT);
  6. // UTF-16 character literal with unicode for `&#229;`:
  7. wchar_t myChar = 0x00E5;
  8. // Adding U+20AC EURO SIGN as well.
  9. wprintf(L&quot;\u20ac%lc&quot;, myChar);
  10. }

Output:

  1. €&#229;

huangapple
  • 本文由 发表于 2023年6月22日 04:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定