Aquarius_Girl
Aquarius_Girl

Reputation: 22946

How to read hex from a file and store it in an unsigned char?

File contains the characters '0', 'x', '5', '1' and a new line.

QFile file ("config.txt");

QString fileContent;
if (file.open (QIODevice::ReadOnly))
{
    QString line;
    QTextStream t(&file);
    do
    {
        line = t.readLine();
        fileContent += line;
    } while (!line.isNull());

    file.close();
}

unsigned char m_pduAddress = fileContent.toUInt (0, 16);

How to read hex from a file and store it in an unsigned char?

All the decimal to hex conversion functions I have heard of use char*. I have to store the result in unsigned char, not in char*.

Upvotes: 0

Views: 1120

Answers (1)

Werner Henze
Werner Henze

Reputation: 16781

You can directly read the value from the QTextStream:

QTextStream t(&file);
int value;
t >> value;

This will automatically detect the leading 0x and read the rest as a hex number.

Upvotes: 2

Related Questions