你好,我想打印字母'å',它在扩展ascii中是131,据我所知,UTF-8代码在UTF-16中是00E5和0x00E5。但是,在下面的代码中,程序打印“Õ”,这不是我想要的。我不知道我做错了什么。提前致谢。
int main(void) {
wchar_t myChar = 0x00E5;
wprintf(L"%lc", myChar);
}
最佳答案
我会尝试使用 UTF-16 字 rune 字,并将区域设置 ( setlocale
) 设置为 CP_UTF_16LE
(如果使用 MSVC),否则设置为用户首选的区域设置。
#ifdef _MSC_VER
#include <io.h> // _setmode
#include <fcntl.h> // _O_U16TEXT
#endif
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
void set_locale_mode() {
#ifdef _MSC_VER
// Unicode UTF-16, little endian byte order (BMP of ISO 10646)
const char *CP_UTF_16LE = ".1200";
setlocale(LC_ALL, CP_UTF_16LE);
_setmode(_fileno(stdout), _O_U16TEXT);
#else
// set the user-preferred locale
setlocale(LC_ALL, "");
#endif
}
int main(void) {
set_locale_mode(); // do this before any other output
// UTF-16 character literal with unicode for `å`:
wchar_t myChar = u'\u00E5';
wprintf(L"%lc", myChar);
}
在 Windows 11 中使用 VS2022 进行测试
关于c - c中打印宽字符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76526768/