UTF 8编码算法与UTF 16算法

时间:2014-05-06 18:23:47

标签: c++ algorithm utf-8 utf-16 utf

我正在尝试使用C ++将unicode字符的十进制值转换为它们的实际字符,我不想使用任何库。我在StackOverflow上由用户给出了下面的函数,它将十进制表示转换为UTF 8字符。

当我在OSX上测试我的代码时,这解决了我的所有问题,但遗憾的是,当我在Windows上测试时输出的字符完全不正确。我现在明白Windows使用UTF 16,这可以解释为什么在Windows上输出错误的字符。

问题是,因为我自己没有写这个函数,所以我不知道它是如何工作的。我已经尝试了谷歌搜索函数的每个不同部分,我知道它是UTF 8编码算法,我知道它使用按位运算,但我不知道它是如何工作的。这是功能:

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';
}
}

所以这是我的问题,有没有人知道将UTF 8编码函数转换为UTF 16编码函数的方法?我对这两种算法做了一些研究,事实是,我也不太了解。

或者我看到人们使用函数MultiByteToWideChar,但我也无法使用它。任何人都可以请给我一个方法或功能,允许我在Windows上显示正确的unicode字符,而无需用户更改其控制台代码页?

1 个答案:

答案 0 :(得分:4)

阅读维基百科上UTF-8UTF-16的描述,他们描述了编码算法。

尝试这样的事情:

void GetUnicodeCharAsUtf8(unsigned int code, char chars[5])
{
    if (code <= 0x7F) {
        chars[0] = (code & 0x7F);
        chars[1] = '\0';
    } else if (code > 0x10FFFF) {
        // unicode replacement character
        chars[0] = 0xEF;
        chars[1] = 0xBF;
        chars[2] = 0xBD;
        chars[3] = '\0';
    } else {
        int count;
        if (code <= 0x7FF) {
            // one continuation byte
            count = 1;
        } else if (code <= 0xFFFF) {
            // two continuation bytes
            count = 2;
        } else {
            // three continuation bytes
            count = 3;
        }
        for (int i = 0; i < count; ++i) {
            chars[count-i] = 0x80 | (code & 0x3F);
            code >>= 6;
        }
        chars[0] = (0x1E << (6-count)) | (code & (0x3F >> count));
        chars[1+count] = '\0';
    }
}

void GetUnicodeCharAsUtf16(unsigned int code, unsigned short chars[2])
{
    if ( ((code >= 0x0000) && (code <= 0xD7FF)) ||
        ((code >= 0xE000) && (code <= 0xFFFF)) )
    {
        chars[0] = 0x0000;
        chars[1] = (unsigned short) code;
    }
    else if ((code >= 0xD800) && (code <= 0xDFFF))
    {
        // unicode replacement character
        chars[0] = 0x0000;
        chars[1] = 0xFFFD;
    }
    else
    {
        // surrogate pair
        code -= 0x010000;
        chars[0] = 0xD800 + (unsigned short)((code >> 10) & 0x3FF);
        chars[1] = 0xDC00 + (unsigned short)(code & 0x3FF);
    }
}