void demo1()
{
QString ss1= "8392183dwediuw%$^\\xe5\\x85\\xa8\\xe6\\x9d\\x91\\xe7\\x9a\\x84\\xe5\\xb8\\x8c\\xe6\\x9c\\x9b8r9u9342234890r\\xe4\\xbd\\xa0";
QString ss2 = "\\xe5\\x85\\xa8\\xe6\\x9d\\x91\\xe7\\x9a\\x84\\xe5\\xb8\\x8c\\xe6\\x9c\\x9b";
QString ss3 = "\\xe5\\x85\\xa8\\xe6\\x9d\\x91\\xe7\\x9a\\x84\\xe5\\xb8\\x8c\\xe6\\x9c\\x9b8932r$#@ "+ss2;
QString ss4 = "3928hr43@#!$%#t"+ss3;
QString ss5 = "1\\xe5\\x85\\xa8\\xe6\\x9d\\x91\\xe7\\x9a\\x84\\xe5\\xb8\\x8c\\xe6\\x9c\\x9b23";
QString ss6 = "1";
QString ss7 = "zx\\xe4\\xbd\\xa0Xyz\\xe4\\xbd\\xa0Abc123\\xe4\\xbd\\xa0";
QString ss8 = "123\\xe4\\xbd\\xa0";
qDebug() << "ss1: " << characterConversion(ss1);
qDebug() << "ss2: " << characterConversion(ss2);
qDebug() << "ss3: " << characterConversion(ss3);
qDebug() << "ss4: " << characterConversion(ss4);
qDebug() << "ss5: " << characterConversion(ss5);
qDebug() << "ss6: " << characterConversion(ss6);
qDebug() << "ss7: " << characterConversion(ss7);
qDebug() << "ss8: " << characterConversion(ss8);
}
demo1的运行结果
ss1: "8392183dwediuw%$^全村的希望8r9u9342234890r你"
ss2: "全村的希望"
ss3: "全村的希望8932r$#@ 全村的希望"
ss4: "3928hr43@#!$%#t全村的希望8932r$#@ 全村的希望"
ss5: "1全村的希望23"
ss6: "1"
ss7: "zx你Xyz你Abc123你"
ss8: "123你"
十六进制转中文,不一定完善,可能还有其他语言、表情包、特殊字符
QString characterConversion(const QString& ss)
{
QString string1;
QString string2;
QString string3;
bool ok;
char ch[512] = { 0 };
QString result = "";
int icount = 0;
for (int i = 0; i < ss.length(); )
{
if ((i + 4) <= ss.length()) {
if (ss.at(i) == '\\' && ss.at(i + 1) == 'x') {
QString tmpstring = QString(ss.at(i + 2)) + ss.at(i + 3);
tmpstring = "0x" + tmpstring;
ch[icount] = static_cast<char>(tmpstring.toInt(&ok, 16));
icount++;
i += 4;
if (string2.size() > 0) {
result += string2;
string2.clear();
}
if (string3.size() > 0) {
result += string3;
string3.clear();
}
} else {
string1 = ch;
if (string1.size() > 0) {
result += string1;
icount = 0;
string1.clear();
memset(ch, 0, sizeof(ch));
}
if (string3.size() > 0) {
result += string3;
string3.clear();
}
string2 = string2 + ss.at(i);
i++;
}
} else {
if (string1.size() > 0) {
result += string1;
icount = 0;
string1.clear();
memset(ch, 0, sizeof(ch));
}
if (string2.size() > 0) {
result += string2;
string2.clear();
}
string3 = string3 + ss.at(i);
i++;
}
}
string1 = ch;
if (string1.size() > 0) {
result += string1;
}
if (string2.size() > 0) {
result += string2;
}
if (string3.size() > 0) {
result += string3;
}
return result;
}