将hex QString转换为UTF-8 QString。

时间:2023-01-06 11:06:38

In my project I need to convert a hex QString ("c2a774657374") into a UTF-8 encoded QString ("§test")

在我的项目我需要转换十六进制QString(“c2a774657374”)转换为utf - 8编码QString(“§测试”)

My current function (HexToAscii) does convert the hex to ascii, but does not encode it properly:

我的当前函数(HexToAscii)确实将十六进制转换为ascii,但没有正确编码:

void HexToInt(QString num_hex, int &num_int)
{
    uint num_uint;
    bool ok;
    num_uint = num_hex.toUInt(&ok,16);
    num_int = (int)num_uint;
}

void HexToAscii(QString &Input)//Input == "c2a774657374"
{
    int inputInt;
    QString tempInput;
    for(int i=0; i<Input.length()/2; i++)
    {
        HexToInt(Input.mid(i*2, 2), inputInt);
        tempInput.append((unsigned char)inputInt);
    }
    Input = tempInput;//Input == "§test"
}

However, this only converts each byte and doesn't follow UTF-8 encoding. The result of my current function is ("§test") when I want it to be ("§test")

但是,这只会转换每个字节,不遵循UTF-8编码。我的当前函数的结果是(§测试”),当我想要(“§测试”)

How can I edit my HexToAscii function to convert to a UTF-8 encoded QString?

我如何编辑我的HexToAscii函数来转换为UTF-8编码的QString?

Thanks for your time.

谢谢你的时间。

2 个解决方案

#1


1  

Well, on UTF-8, up to 127 it is single byte. The problem is how QString deals with two byte sequences.

嗯,在UTF-8上,最多127个字节。问题是QString如何处理两个字节序列。

The QString cannot tell what "c2" is and proceed to allocate instead to wait for the next byte.

QString不知道“c2”是什么,然后继续分配,以等待下一个字节。

You can fix this by checking if this hex returned is greater than 127 (7f) and concatenating it with the next Hex before appending to QString.

您可以通过检查这个十六进制是否返回大于127 (7f),并在追加到QString之前将它与下一个hex连接起来,从而修复这个问题。

Try:

试一试:

#include <QCoreApplication>
#include <iostream>


void HexToInt(QString num_hex, int &num_int)
{
    uint num_uint;
    bool ok;
    num_uint = num_hex.toUInt(&ok,16);
    num_int = (int)num_uint;
}

QString HexToAscii(QString Input)//Input == "c2a774657374"
{
    int inputInt;
    int twobytesequece;
    QString tempInput;
    for(int i=0; i<Input.length()/2; i++)
    {
        HexToInt(Input.mid(i*2, 2), inputInt);
        if (inputInt <= 0x7f) {
            tempInput.append((unsigned char)inputInt);
        } else {
            i++;
            HexToInt(Input.mid(i*2, 2), twobytesequece);
            inputInt = inputInt << 8 | twobytesequece;
            tempInput.append((unsigned char)inputInt);
        }
    }
    return tempInput;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString test = "c2a774657374";
    test = HexToAscii(test);

    return a.exec();
}

#2


2  

To get a decoded copy of the hex encoded string, you can use QByteArray::fromHex() static function.

要获得十六进制编码字符串的解码副本,您可以使用QByteArray::fromHex()静态函数。

QByteArray hex = QByteArray::fromHex("c2a774657374");
QString str = QString::fromUtf8(hex);

#1


1  

Well, on UTF-8, up to 127 it is single byte. The problem is how QString deals with two byte sequences.

嗯,在UTF-8上,最多127个字节。问题是QString如何处理两个字节序列。

The QString cannot tell what "c2" is and proceed to allocate instead to wait for the next byte.

QString不知道“c2”是什么,然后继续分配,以等待下一个字节。

You can fix this by checking if this hex returned is greater than 127 (7f) and concatenating it with the next Hex before appending to QString.

您可以通过检查这个十六进制是否返回大于127 (7f),并在追加到QString之前将它与下一个hex连接起来,从而修复这个问题。

Try:

试一试:

#include <QCoreApplication>
#include <iostream>


void HexToInt(QString num_hex, int &num_int)
{
    uint num_uint;
    bool ok;
    num_uint = num_hex.toUInt(&ok,16);
    num_int = (int)num_uint;
}

QString HexToAscii(QString Input)//Input == "c2a774657374"
{
    int inputInt;
    int twobytesequece;
    QString tempInput;
    for(int i=0; i<Input.length()/2; i++)
    {
        HexToInt(Input.mid(i*2, 2), inputInt);
        if (inputInt <= 0x7f) {
            tempInput.append((unsigned char)inputInt);
        } else {
            i++;
            HexToInt(Input.mid(i*2, 2), twobytesequece);
            inputInt = inputInt << 8 | twobytesequece;
            tempInput.append((unsigned char)inputInt);
        }
    }
    return tempInput;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString test = "c2a774657374";
    test = HexToAscii(test);

    return a.exec();
}

#2


2  

To get a decoded copy of the hex encoded string, you can use QByteArray::fromHex() static function.

要获得十六进制编码字符串的解码副本,您可以使用QByteArray::fromHex()静态函数。

QByteArray hex = QByteArray::fromHex("c2a774657374");
QString str = QString::fromUtf8(hex);