当前位置:网站首页>Qt Utf8 与 Unicode 编码的互相转换, Unicode编码输出为格式为 &#xXXXX
Qt Utf8 与 Unicode 编码的互相转换, Unicode编码输出为格式为 &#xXXXX
2022-06-25 22:00:00 【北极熊的奋斗史】
先上结果:
utf-8文字: 这是测试文字123456abcdefg
Unicode码: 杩欐槸娴嬭瘯鏂囧瓧123456abcdefg
上代码:
1. utf8转Unicode码
QString utf8ToUnicode(const QString strUtf8)
{
QString strOut;
QString unidata = strUtf8;
for (int i = 0; i < unidata.length(); ++i)
{
ushort num = unidata[i].unicode();
if (num < 255)
strOut += unidata[i];
else
strOut += QString("&#x%1;").arg(QString::number(num, 16));
}
return strOut;
}
2. Unicode转utf8
QString unicodeToUtf8(const QString strUnicode)
{
QString strOut = strUnicode;
int nPos = 0;
QRegExp rx("&#x([0-9,a-f|A-F]{4});");
while ((nPos = rx.indexIn(strOut, nPos)) != -1)
{
QChar qCh(rx.cap(1).toUShort(nullptr, 16));
strOut.replace(nPos, rx.matchedLength(), qCh);
nPos += 1;
}
return strOut;
}
使用代码:
QString str = QString::fromLocal8Bit("5.6破坏的房屋和街区.svg");
QString strUnicode = utf8ToUnicode(str);
QString strUtf8 = unicodeToUtf8(strUnicode);
边栏推荐
- CDN加速是什么
- Implementation of sequence table: static and dynamic
- Ble Low Power Bluetooth networking process and Bluetooth role introduction
- cookie、session、token
- 牛客小白月賽52--E 分組求對數和(二分)
- How to download the software package of CDH version
- Oracle -- table operation
- Basic operator
- 最近准备翻译外国优质文章
- Kubernetes cluster construction of multiple ECS
猜你喜欢
Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!
做接口测试,这3种工具到底什么时候用?
【AXI】解读AXI协议原子化访问
UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别
电路模块分析练习6(开关)
Meta universe standard forum established
我的vscode
Paper notes: multi tag learning MSWl
RK3568+鸿蒙工控板工业网关视频网关解决方案
LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
随机推荐
UE4_ Ue5 combines the offline voice recognition plug-in for speech recognition
What is Unified Extensible Firmware Interface (UEFI)?
How to add cartoon characters to the blog park?
Fastjson deserialization randomness failed
OpenJudge NOI 2.1 15:Counterfeit Dollar
[untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
Technology blog site collection
CTS RTS RX TX in serial port flow control UART (direct communication between serial port module and MCU)
Idea shortcut
character string
pdm导入vscode的实现方式
电路模块分析练习5(电源)
ES6 - numerical extension and object extension
记录一下Qt将少量图片输出为MP4的思路及注意事项
字符串
The Ping class of unity uses
UE4_UE5結合offline voice recognition插件做語音識別功能
做接口测试,这3种工具到底什么时候用?
Why is the frame rate calculated by opencv wrong?
Meta universe standard forum established