Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What does fromHex do?
Qt 6.11 is out! See what's new in the release blog

What does fromHex do?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    Understand tohex caused number2 to be display as hexadecimal.
    qDebug()<<"number2"<<number2.toHex();

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      [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]

      1 Reply Last reply
      0
      • H Offline
        H Offline
        houmingc
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          Example:
          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".

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved