c - c中打印宽字符的问题

标签 c unicode widechar

你好,我想打印字母'å',它在扩展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/

相关文章:

c - 使用 C 结构

javascript - 如何将 Canvas 像素数组传递给 C++ 代码并将其保存为 jpeg?

c++ - 使用 AVX2 时矩阵乘法代码运行速度较慢

python - 使用 json.dumps() 时出现 UnicodeDecodeError

c++ - WideCharToMultiByte() 与 wcstombs()

c - 隐藏 C 结构定义

c++ - C++ 编译器如何从 utf8 源文件生成 unicode 字符串文字

html - 如何从动态列表的最后一个元素中删除 html unicode 内容?

c++ - 没有空格的宏

c++ - 在特定的 Unicode 路径中创建文件