VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1217|回复: 1

求个简洁的c++ unicode码转中文字符的方法源代码案例

[复制链接]
97_avatar_middle
最佳答案
0 
donate_vip 发表于 2023-6-25 12:40:49 | 显示全部楼层 |阅读模式

最近几天在爬网,遇上如下的混合数据内容,想转换成中文字符串:


["801085","\u4eba\u5de5\u667a\u80fd",14390],["801001","VMD",4228,],["801128","\u56fd\u4ea7\u8f6f\u4ef6",3385]


转换后的文本是需要是这样子的:["801085","人工智能",14390],["801001","VMD",4228,],["801128","国产软件",3385]

有请哪位大神帮忙弄个简洁的c++ 中文字符的方法源代码案例,我是c++2017的,先道谢了!




上一篇:WSAAsyncSelect
下一篇:raw sock关联icop 接收不了数据
97_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2023-6-25 13:29:14 | 显示全部楼层
#include <atlstr.h>
   //需要项目属性:项目属性-配置属性-高级-字符集 :使用多字节字符集
string unicodeToCHI2(string& unicodeStr)
{
        int index;
        CString cstr;
        int nValue = 0;
        WCHAR* pWchar;
        wchar_t* szHex;
        char strchar[6] = { '0', 'x', '\0' };

        if (unicodeStr.length() <= 0)
        {
                return "";
        }
                while (1) {
                int index = unicodeStr.find("\\u");
                if (index <0 || index>unicodeStr.length())
                        break;

                strchar[2] = unicodeStr[index + 2];
                strchar[3] = unicodeStr[index + 3];
                strchar[4] = unicodeStr[index + 4];
                strchar[5] = unicodeStr[index + 5];
                                USES_CONVERSION;
                szHex = A2W(strchar);
                                StrToIntExW(szHex, STIF_SUPPORT_HEX, &nValue);
                pWchar = (WCHAR*)&nValue;
                cstr = pWchar;
                //替换掉unicode字符串
                unicodeStr.replace(index, 6, cstr);
        }
        return unicodeStr;
}


已自己解决
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-9-24 21:31

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表