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. unsigned short to unsigned char array in hex format
Forum Updated to NodeBB v4.3 + New Features

unsigned short to unsigned char array in hex format

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.4k Views
  • 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.
  • K Offline
    K Offline
    Kashif
    wrote on last edited by Kashif
    #1

    Hi All,

    I have to send the message to server with following specs:
    lengthHEX(2Bytes)+message

    using MinGW on devices with embedded linux we use following code (in C), on our new device with QT environment using ARM unknown linux compiler I am unable to find the solution to perform this task, can anyone kindly help me to solve this issue.

    For your reference I am pasting the working code of MinGW:

    typedef unsigned short UWORD;
    typedef unsigned char UBYTE;

    void Calc2BytesHexLength(UBYTE * outBuf, UWORD len)
    {
    char buf[4];
    sprintf(buf, "%04x" , len);
    AscHex (outBuf,buf,2);
    }

    thanks

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      QByteArray message("hello world!");
      const QByteArray hexLen = QByteArray::number(static_cast<quint16>(message.size()),16);
      message.prepend(hexLen);
      if(hexLen.size()==1)
      message.prepend('0');
      qDebug() << message; //print the formatted message
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      6
      • K Offline
        K Offline
        Kashif
        wrote on last edited by Kashif
        #3

        Hello Ronin,

        Thanks for your reply and solution, I will look into this solution and close accordingly.

        Thanks

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kashif
          wrote on last edited by
          #4

          Hello Ronin,

          Thanks for your response on this query, and sorry for my late reply.
          The solution you pasted above is good for the values those are already in string form, whereas I need the hex values that I have done by using arithmetic division and remainder logic.

          One more thing similar to this is that I need to print/debug the hex value of variable:
          like : BYTE data = 0x01; // (where byte = unsigned char)
          now I need to print on console as:
          data [01]
          with MinGW we use this using:

          UBYTE data = 0x01;
          UBYTE debugData[50];
          memset(debugData, 0, 50);
          //----
          sprintf(debugData, "Data [%02X]", data);
          printf(debugData);

          1 Reply Last reply
          0
          • aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Kashif: If your data is in a QByteArray, toHex() will help you.

            I've changed @VRonin's suggestion a bit:

            QByteArray binary("\x12\x34\x56\x78\x9A\xBC");
            QByteArray message = binary.toHex(); //= "123456789abc"
            const QByteArray hexLen = QByteArray::number(message.size(),16);
            message.prepend(hexLen);
            if(hexLen.size()==1)
              message.prepend('0');
            qDebug() << message; //print the formatted message
            

            Notes:

            1. I've removed the static_cast as I don't think it is needed for QByteArray::number()
            2. Unfortunately QByteArray::number() doesn't have a field width specifier (unlike e.g. QString::arg() has), so printing sizes below 10 needs the ugly prepend('0')
            3. If message is very long, prepending the lenght may be unperformant. In this case, creating a new byte array and appending may be better.
            4. If you need uppercase hex chars, call binary.toHex().toUpper()

            Qt has to stay free or it will die.

            K 1 Reply Last reply
            1
            • aha_1980A aha_1980

              @Kashif: If your data is in a QByteArray, toHex() will help you.

              I've changed @VRonin's suggestion a bit:

              QByteArray binary("\x12\x34\x56\x78\x9A\xBC");
              QByteArray message = binary.toHex(); //= "123456789abc"
              const QByteArray hexLen = QByteArray::number(message.size(),16);
              message.prepend(hexLen);
              if(hexLen.size()==1)
                message.prepend('0');
              qDebug() << message; //print the formatted message
              

              Notes:

              1. I've removed the static_cast as I don't think it is needed for QByteArray::number()
              2. Unfortunately QByteArray::number() doesn't have a field width specifier (unlike e.g. QString::arg() has), so printing sizes below 10 needs the ugly prepend('0')
              3. If message is very long, prepending the lenght may be unperformant. In this case, creating a new byte array and appending may be better.
              4. If you need uppercase hex chars, call binary.toHex().toUpper()
              K Offline
              K Offline
              Kashif
              wrote on last edited by
              #6

              @aha_1980 : Thanks for your reply, my current problem is solved,

              1 Reply Last reply
              1

              • Login

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