C/CPP UTF8编码转成汉字 \u7528\u6237\u4e0d
来源:互联网 发布:淘宝交易关闭欺诈风险 编辑:程序博客网 时间:2024/05/29 16:02
参考文章:http://www.qingfengju.com/article.asp?id=245
现在的网站,经常返回下面这样的字符串:
"error":"\u7528\u6237\u4e0d\u5b58\u5728\u6216\u5bc6\u7801\u9519\u8bef"}
其中的\u7528等就是汉字的UTF8编码了,如何将其还原成相应的字符呢?
代码如下:
#include <string>
using std::string;
string Utf8Code2String(char* szCode){string strRet = "";for (int i = 0; i < 4; i++){if (szCode[i] >= '0' && szCode[i] <= '9')continue;if (szCode[i] >= 'A' && szCode[i] <= 'F')continue;if (szCode[i] >= 'a' && szCode[i] <= 'f')continue;return strRet;}char unicode_hex[5] = {0};memcpy(unicode_hex, szCode, 4);unsigned int iCode = 0;sscanf_s(unicode_hex,"%04x", &iCode);wchar_t wchChar[4] = {0};wchChar[0] = iCode;char szAnsi[8] = {0};WideCharToMultiByte(CP_ACP, NULL, wchChar, 1, szAnsi, sizeof(szAnsi), NULL, NULL);strRet = string(szAnsi);return strRet;}string MyUnEscape(char* szUtf8Code){// {"error":"\u7528\u6237\u4e0d\u5b58\u5728\u6216\u5bc6\u7801\u9519\u8bef"}string strRet = "";char* szDelimiters = "\\u";char* pTok = strtok(szUtf8Code, szDelimiters); while(pTok != NULL){/*OutputDebugString(pTok);OutputDebugString("\r\n");*/if (strlen(pTok) >= 4){strRet += Utf8Code2String(pTok);}pTok = strtok(NULL, szDelimiters);}return strRet;}
// 调用例子
MyUnEscape("test \u7528\u6237\u4e0d\u5b58\u5728\u6216\u5bc6\u7801\u9519\u8bef hahah ok");
贴下参考文章的代码:
#include <string.h> #include <iostream>#include <string>using namespace std; #include <atlconv.h> enum{ UNICODE_CALC_SIZE = 1, UNICODE_GET_BYTES = 2}; //将unicode转义字符序列转换为内存中的unicode字符串int unicode_bytes(char* p_unicode_escape_chars,wchar_t *bytes,int flag){ /* char* p_unicode_escape_chars="pp\\u4fddp\\u5b58\\u6210pp\\u529f0a12"; //通过此函数获知转换后需要的字节数 int n_length=unicode_bytes(p_unicode_escape_chars,NULL,UNICODE_CALC_SIZE); //再次调用此函数,取得字节序列 wchar_t *bytes=new wchar_t[n_length+sizeof(wchar_t)]; unicode_bytes(p_unicode_escape_chars,bytes,UNICODE_GET_BYTES); bytes[n_length]=0; //此时的bytes中是转换后的字节序列 delete[] bytes; */ int unicode_count=0; int length=strlen(p_unicode_escape_chars); for (int char_index=0;char_index<length;char_index++) { char unicode_hex[5]; memset(unicode_hex,0,5); char ascii[2]; memset(ascii,0,2); if (*(p_unicode_escape_chars+char_index)=='\\') { char_index++; if (char_index<length) { if (*(p_unicode_escape_chars+char_index)=='u') { if (flag==UNICODE_GET_BYTES) { memcpy(unicode_hex,p_unicode_escape_chars+char_index+1,4); //sscanf不可以使用unsigned short类型 //否则:Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted. unsigned int a=0; sscanf_s(unicode_hex,"%04x",&a); bytes[unicode_count++]=a; } else if(flag==UNICODE_CALC_SIZE) { unicode_count++; } char_index+=4; } } } else { if (flag==UNICODE_GET_BYTES) { memcpy(ascii,p_unicode_escape_chars+char_index,1); unsigned int a=0; sscanf_s(ascii,"%c",&a); bytes[unicode_count++]=a; } else if(flag==UNICODE_CALC_SIZE) { unicode_count++; } } } return unicode_count;} string UnEscape(char* p_unicode_escape_chars){ int nBytes=unicode_bytes(p_unicode_escape_chars,NULL,UNICODE_CALC_SIZE); wchar_t *p_bytes=new wchar_t[nBytes+sizeof(wchar_t)]; unicode_bytes(p_unicode_escape_chars,p_bytes,UNICODE_GET_BYTES); p_bytes[nBytes]=0; USES_CONVERSION; string cs_return=W2A((wchar_t*)p_bytes); delete[] p_bytes; return cs_return;} int _tmain(int argc, _TCHAR* argv[]){ // 发送成功条 // \u53d1\u9001\u6210\u529f1\u6761 char* p_unicode_escape_chars="\\u53d1\\u9001\\u6210\\u529f1\\u6761"; cout<<UnEscape(p_unicode_escape_chars)<<endl; return 0;}
0 0
- C/CPP UTF8编码转成汉字 \u7528\u6237\u4e0d
- NSURL汉字UTF8编码
- 使用编码格式转换工具将cpp文件由GB18030转成UTF8
- mysql C编程-utf8编码,汉字和blob类型
- vs中cpp文件编码格式为utf8,注释有汉字,编译不通过的问题
- utf8编码-汉字几字节
- 把 GB2312 编码转成汉字
- 把phpwind utf8 转成 gbk 编码
- utf8编码文件批量转成gbk格式
- java中GBK编码格式转成UTF8
- java中GBK编码格式转成UTF8
- 汉字UTF8编码占用几个字节
- 中文汉字 转成数字 编码 方法【转】
- Elixir中将GB2312编码的汉字转为UTF8编码
- UTF8-CPP
- asp.ent把接收过来的gb2312编码转成utf8
- UTF8编码转Unicode编码(C语言)
- UTF8编码转Unicode编码(C语言)
- 笔记,c++中静态对象在程序结束的时候会不会进析构函数?
- 数据库
- this关键字
- 找出有序数组中和等于指定数的两个数
- 【杭电oj2027】统计元音
- C/CPP UTF8编码转成汉字 \u7528\u6237\u4e0d
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1003 Magic boy Bi Luo with his excited tree hdu5834
- 【SDOI2008】【BZOJ2186】沙拉公主的困惑
- AngularJS入门笔记(2)
- Python入门学习笔记(1):Python、PyCharm 的安装和介绍
- Kylin build cube step 2 报错(Kylin 安装在slave 节点上)
- 【杭电oj2028】Lowest Common Multiple Plus
- 英语学习——纤手香凝
- Android-自定义popupwindow详解(包含动画简单讲解)