将qstring转换为const unsigned char数组

时间:2016-03-23 10:07:02

标签: qt qstring

我有一个QString

string = "131865EDC62E4AC5131865EDC62E4AC5"

是16个十六进制数。我需要将其转换为类似

的内容

const unsigned char s[] = {0x13, 0x18, 0x65, 0xED, 0xC6, 0x2E, 0x4A, 0xC5, 0x13, 0x18, 0x65, 0xED, 0xC6, 0x2E, 0x4A, 0xC5}

我该怎么做?我尝试了不同的方法,如QString::toInt, QString::toUlong, QString::toLatin1,但他们没有做我想做的事。

1 个答案:

答案 0 :(得分:-1)

这就是你想要的吗?

QString string = "131865EDC62E4AC5131865EDC62E4AC5";

QString sub = string.left(2) ;
QList<unsigned char> list;
while(sub != "")
{
    bool bStatus = false;
    unsigned int res = sub.toUInt(&bStatus,16);
    if (bStatus)
        list.append(res);

    string = string.mid(2);
    sub = string.left(2) ;
}

QVector<unsigned char> vect = list.toVector();
const unsigned char* s = vect.constData();

for (auto it = vect.begin(); it != vect.end(); ++it)
    printf("%#x ",*it);