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. how to put hex numbers into a Qbytearray?
Qt 6.11 is out! See what's new in the release blog

how to put hex numbers into a Qbytearray?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 5.1k 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.
  • N Offline
    N Offline
    Nimika
    wrote on last edited by kshegunov
    #1

    Hello,
    Can anyone suggest me about how to use hex number which are in ASCII form as a Qbytearray?
    Actually I am converting my decimal number into hex number which is then stored as its ascii character using this function

    QByteArray double2HexString(double n)
    {   
       unsigned char hexaDeciNum[20];
       QByteArray hexbyte ;
        int i = 0;
        while(n!=0)
        {   
            int temp  = 0;
            temp = n % 16;
            if(temp < 10)
            {
                hexaDeciNum[i] = temp + 48;
                i++;
            }
            else
            {
                hexaDeciNum[i] = temp + 55;
                i++;
            }
            n = n/16;
        }
          for(int j=i-1; j>=0; j--)
            cout << hexaDeciNum[j];
    
    hexbyte = QByteArray((char*)hexDeciNum,20);
    return hexbyte;
    }
    

    which then I need to send to another function which is storing the hex number into an array using this function byte by byte.

    void writeout_RS5(){
       QByteArray writedata_RS5;
    QByteArray value;
         qDebug()<<"helloooooooooo";
        writedata_RS5=double2HexString(data);
        value[0]=  writedata_RS5[0];
        value[1]= writedata_RS5[1];
        qDebug()<<writedata_RS5.toHex();
    

    Where m I wrong?
    What can I do now?
    Can anyone help?

    [Added code tags ~kshegunov]

    jsulmJ 1 Reply Last reply
    0
    • N Nimika

      Hello,
      Can anyone suggest me about how to use hex number which are in ASCII form as a Qbytearray?
      Actually I am converting my decimal number into hex number which is then stored as its ascii character using this function

      QByteArray double2HexString(double n)
      {   
         unsigned char hexaDeciNum[20];
         QByteArray hexbyte ;
          int i = 0;
          while(n!=0)
          {   
              int temp  = 0;
              temp = n % 16;
              if(temp < 10)
              {
                  hexaDeciNum[i] = temp + 48;
                  i++;
              }
              else
              {
                  hexaDeciNum[i] = temp + 55;
                  i++;
              }
              n = n/16;
          }
            for(int j=i-1; j>=0; j--)
              cout << hexaDeciNum[j];
      
      hexbyte = QByteArray((char*)hexDeciNum,20);
      return hexbyte;
      }
      

      which then I need to send to another function which is storing the hex number into an array using this function byte by byte.

      void writeout_RS5(){
         QByteArray writedata_RS5;
      QByteArray value;
           qDebug()<<"helloooooooooo";
          writedata_RS5=double2HexString(data);
          value[0]=  writedata_RS5[0];
          value[1]= writedata_RS5[1];
          qDebug()<<writedata_RS5.toHex();
      

      Where m I wrong?
      What can I do now?
      Can anyone help?

      [Added code tags ~kshegunov]

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Nimika There is a much much easier way to convert numbers to hex: http://doc.qt.io/qt-5/qstring.html#number

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Nimika There is a much much easier way to convert numbers to hex: http://doc.qt.io/qt-5/qstring.html#number

        M Offline
        M Offline
        mostefa
        wrote on last edited by
        #3

        @jsulm said in how to put hex numbers into a Qbytearray?:

        @Nimika There is a much much easier way to convert numbers to hex: http://doc.qt.io/qt-5/qstring.html#number

        Or even a QByteArray::number since you want to store the result in a QByteArray

        J.HilkJ 1 Reply Last reply
        2
        • M mostefa

          @jsulm said in how to put hex numbers into a Qbytearray?:

          @Nimika There is a much much easier way to convert numbers to hex: http://doc.qt.io/qt-5/qstring.html#number

          Or even a QByteArray::number since you want to store the result in a QByteArray

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @mostefa if we go that far, QByteArray accepts an int value as constructor:

          QByteArray byteArray1(61455);
          //or
          QByteArray byteArray2(0xF00F);
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          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