• Unicode码转UTF8


    转换代码

    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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
  • 相关阅读:
    Flask 学习-34.restful-full 请求参数自定义参数校验类型 (reqparse.RequestParser() )
    C语言学习笔记
    一个翻翻的小游戏。HTML式。
    eclipse中创建并运行Filter和Listener
    新版IDEA没有办法选择Java8版本解决方法
    基于粒子滤波和帧差法的目标跟踪matlab仿真
    利用go制作微信机器人
    HPLC电力载波灯控的节能照明 智慧照明方案
    信号本无事,庸人自扰之
    pdf如何让多张图片在一页
  • 原文地址:https://blog.csdn.net/s634772208/article/details/126508183