C++ Windows十进制到UTF-8字符转换

Fra*_*cis 1 c++ windows unicode winapi utf-8

我一直在使用下面的函数将unicode字符的十进制表示转换为C++中的UTF8字符本身.我目前的功能在Linux/Unix系统上运行良好,但它在Windows上不断返回错误的字符.

void GetUnicodeChar(unsigned int code, char chars[5]) {
    if (code <= 0x7F) {
        chars[0] = (code & 0x7F); chars[1] = '\0';
    } else if (code <= 0x7FF) {
        // one continuation byte
        chars[1] = 0x80 | (code & 0x3F); code = (code >> 6);
        chars[0] = 0xC0 | (code & 0x1F); chars[2] = '\0';
    } else if (code <= 0xFFFF) {
        // two continuation bytes
        chars[2] = 0x80 | (code & 0x3F); code = (code >> 6);
        chars[1] = 0x80 | (code & 0x3F); code = (code >> 6);
        chars[0] = 0xE0 | (code & 0xF); chars[3] = '\0';
    } else if (code <= 0x10FFFF) {
        // three continuation bytes
        chars[3] = 0x80 | (code & 0x3F); code = (code >> 6);
        chars[2] = 0x80 | (code & 0x3F); code = (code >> 6);
        chars[1] = 0x80 | (code & 0x3F); code = (code >> 6);
        chars[0] = 0xF0 | (code & 0x7); chars[4] = '\0';
    } else {
        // unicode replacement character
        chars[2] = 0xEF; chars[1] = 0xBF; chars[0] = 0xBD;
        chars[3] = '\0';
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供替代功能或修复我正在使用的当前功能,将在Windows上运行吗?

--UPDATE--

INPUT: 225
OUTPUT ON OSX: á
OUTPUT ON WINDOWS: ?í
Run Code Online (Sandbox Code Playgroud)

bam*_*s53 5

你没有显示你的打印代码,但可能你做的是这样的事情:

char s[5];
GetUnicodeChar(225, s);
std::cout << s << '\n';
Run Code Online (Sandbox Code Playgroud)

你在OS X上获得正常输出和在Windows上输出错误的原因是因为OS X使用UTF-8作为默认编码,而Windows使用一些传统编码.因此,当您在OS X上输出UTF-8时,OS X会(正确地)假设它是UTF-8并显示它.在Windows上输出UTF-8时,Windows会(错误地)假定它是其他编码.

您可以使用iconv程序在Terminal.app中使用以下命令模拟OS X上的问题

iconv -f cp437 -t utf8 <<< "á" 
Run Code Online (Sandbox Code Playgroud)

这将获取UTF-8字符串,将其重新解释为使用Windows代码页437编码的字符串,并将其转换为UTF-8进行显示.OS X上的输出是.

为了测试小东西,您可以执行以下操作以在Windows上正确显示UTF-8数据.

#include <Wincon.h>

#include <cstdio>

char s[5];
GetUnicodeChar(225, s);

SetConsoleOutputCP(CP_UTF8);
std::printf("%s\n", s);
Run Code Online (Sandbox Code Playgroud)

另外,Windows的部分标准库的实现不支持UTF-8的输出,因此即使更改输出编码代码之后std::cout << s仍然无法正常工作.


在旁注中,将数组作为参数,如下所示:

void GetUnicodeChar(unsigned int code, char chars[5]) { 
Run Code Online (Sandbox Code Playgroud)

是个坏主意.这不会发现错误,例如:

char *s; GetUnicodeChar(225, s);
char s[1]; GetUnicodeChar(225, s);
Run Code Online (Sandbox Code Playgroud)

您可以通过更改函数来引用数组来避免这些特定问题:

void GetUnicodeChar(unsigned int code, char (&chars)[5]) { 
Run Code Online (Sandbox Code Playgroud)

但总的来说,我建议完全避免使用原始数组.std::array如果你真的想要一个数组,你可以使用.您可以使用,std::string如果您想要文本,IMO在这里是一个不错的选择:

std::string GetUnicodeChar(unsigned int code);
Run Code Online (Sandbox Code Playgroud)