Qt World Summit: Register Today!

What does fromHex do?

  • Understand tohex caused number2 to be display as hexadecimal.

    What does fromHex do?
    QByteArray ba = QByteArray::fromHex("05");

  • [quote]Returns a decoded copy of the hex encoded array hexEncoded. Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

    For example:
    QByteArray text = QByteArray::fromHex("517420697320677265617421");
    text.data(); // returns "Qt is great!"
    See also toHex().[/quote]

  • I seen that post. in layman terms please.
    Why is it used & how to use it?

  • If you have a byte array (a bunch of raw data, really) that you want to make fit for display as text, you can encode it as hexadecimal values using QByteArray::toHex(). That will mean that every byte is encoded as two characters from the range 0-9A-F.

    QByteArray::fromHex() does the reverse: it interprets a byte array as a set of characters where every two characters together encode a single byte again. Any character in the input other than those in the range 0-9A-F is just skipped.

    Note that the character range 0-9A-F are characters, that is, values 48-57 and 65-70.

    The QByteArray containing these values (as 3 bytes): 256, 77, 23
    Written in hex, that would be FF 4D 17 (that is: 3 values, written as 6 characters)
    would after hex encoding it would then be: (as 6 bytes (chars)): 70, 70, 52, 68, 50, 55

    If you'd print that using qDebug(), or convert it into a QString, it would read "FF4D17".

Log in to reply