将QString转换为Ascii值&Qt中的Versa副手 [英] Converting QString to Ascii value & Vice Versa in Qt

查看:57
本文介绍了将QString转换为Ascii值&Qt中的Versa副手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QString StrData ="abcd" ,我想获取该字符串的十六进制 Ascii 值,反之亦然.

I have a QString StrData = "abcd" and I want get the Ascii value in hex of that string and Vice Versa.

例如,从"abcd"到"61 62 63 64" ,从"61 62 63 64"到"abcd"

我设法以十六进制获取了Ascii值,但不知道如何找回

I manage to get the Ascii value in hex but don't know how to get it back

Qstring StrData = "abcd";
Qstring HexStrData;
for (int i = 0; i < StrData.length(); i++) {
    HexStrData.append(Qstring::number(StrData.at(i).unicode(), 16));
    HexStrData.append(" ");
}

推荐答案

要进行第一次转换,可以使用以下方法:

To do the first conversion you can use the following method:

QString StrData = "abcd";
qDebug()<<"before "<< StrData;
QStringList numberString;
for(const auto character: StrData){
    numberString << QString::number(character.unicode(), 16);
}
QString HexStrData= numberString.join(" ");

qDebug()<<HexStrData;

第二种情况要简单得多,如下所示:

For the second case is much simpler as I show below:

QString str = QByteArray::fromHex(HexStrData.remove(" ").toLocal8Bit());
qDebug()<<str;

输出:

before  "abcd"
"61 62 63 64"
"abcd"

这篇关于将QString转换为Ascii值&amp;Qt中的Versa副手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆