整型数字转utf8

Posted yuanxiaoping_21cn_com

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整型数字转utf8相关的知识,希望对你有一定的参考价值。

static std::string codePointToUTF8(unsigned int cp)
{
std::string result;

// based on description from http://en.wikipedia.org/wiki/UTF-8

if (cp <= 0x7f)
{
result.resize(1);
result[0] = static_cast<char>(cp);
}
else if (cp <= 0x7FF)
{
result.resize(2);
result[1] = static_cast<char>(0x80 | (0x3f & cp));
result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
}
else if (cp <= 0xFFFF)
{
result.resize(3);
result[2] = static_cast<char>(0x80 | (0x3f & cp));
result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6)));
result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12)));
}
else if (cp <= 0x10FFFF)
{
result.resize(4);
result[3] = static_cast<char>(0x80 | (0x3f & cp));
result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
}

return result;
}

以上是关于整型数字转utf8的主要内容,如果未能解决你的问题,请参考以下文章

罗马数字转整型

字符串转整型数字

java中将map转成json时,如何将map中的整型数字在转成json后,变成字符串,而不是整型。

C语言itoa()函数和atoi()函数详解(整数转字符C实现)

arduino int转unsigned int

JS中日期型与长整型是如何转换的