转换代码
Unicode码查询网站:https://www.qqxiuzi.cn/zh/unicode-zifu.php
//U-00000000 - U-0000007F : 0xxxxxxx
//U-00000080 - U-000007FF : 110xxxxx 10xxxxxx
//U-00000800 - U-0000FFFF : 1110xxxx 10xxxxxx 10xxxxxx
//U-00010000 - U-001FFFFF : 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//U-00200000 - U-03FFFFFF : 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
//U-04000000 - U-7FFFFFFF : 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
std::string UnicodeToUtf8(unsigned int nUnicode)
{
//1111110x FC
//111110xx F8
//11110xxx F0
//1110xxxx E0
//110xxxxx C0
if (nUnicode <= 0x7F) //1字节
{
std::string strTemp;
strTemp.push_back(char(nUnicode));
return strTemp;
}
else if (nUnicode <= 0x7FF) //2字节
{
char utf8[2 + 1] = { 0 };
utf8[0] = 0xC0 | (nUnicode >> 6);
utf8[1] = 0x80 | (nUnicode & 0x3F);
return std::string(utf8);
}
else if (nUnicode <= 0xFFFF) //3字节
{
char utf8[3 + 1] = { 0 };
utf8[0] = 0xE0 | (nUnicode >> 12);
utf8[1] = 0x80 | ((nUnicode >> 6) & 0x3F);
utf8[2] = 0x80 | (nUnicode & 0x3F);
return std::string(utf8);
}
else if (nUnicode <= 0X1FFFFF) //4字节
{
char utf8[4 + 1] = { 0 };
utf8[0] = 0xF0 | (nUnicode >> 18);
utf8[1] = 0x80 | ((nUnicode >> 12) & 0x3F);
utf8[2] = 0x80 | ((nUnicode >> 6) & 0x3F);
utf8[3] = 0x80 | (nUnicode & 0x3F);
return std::string(utf8);
}
else if (nUnicode <= 0x3FFFFFF) //5字字
{
char utf8[5 + 1] = { 0 };
utf8[0] = 0xF8 | (nUnicode >> 24);
utf8[1] = 0x80 | ((nUnicode >> 18) & 0x3F);
utf8[2] = 0x80 | ((nUnicode >> 12) & 0x3F);
utf8[3] = 0x80 | ((nUnicode >> 6) & 0x3F);
utf8[4] = 0x80 | (nUnicode & 0x3F);
return std::string(utf8);
}
else //6字节
{
char utf8[6 + 1] = { 0 };
utf8[0] = 0xFC | (nUnicode >> 30);
utf8[1] = 0x80 | ((nUnicode >> 24) & 0x3F);
utf8[2] = 0x80 | ((nUnicode >> 18) & 0x3F);
utf8[3] = 0x80 | ((nUnicode >> 12) & 0x3F);
utf8[4] = 0x80 | ((nUnicode >> 6) & 0x3F);
utf8[5] = 0x80 | (nUnicode & 0x3F);
return std::string(utf8);
}
}