Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray : Converts data in bytes.
Qt 6.12 LTS is out! See what's new in the release blog

QByteArray : Converts data in bytes.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 641 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
    Kycho
    wrote on last edited by Kycho
    #1

    Hello everyone.

    I'm having trouble converting to Hex.
    First is the code.

        /* Error Code */
        int n1 = 1;
        QByteArray val1 = QByteArray("1B33")+QByteArray::number(n1,16); // val1 : 1B331
        qDebug() << QByteArray::fromHex(val1); //result : "\x01\xB3\x31" ( I do not want this value )
        // I want result : "\x1B\x33\x01"
    
        /*Good work Code*/
        int n2 = 20;
        QByteArray val2 = QByteArray("1B33")+QByteArray::number(n2,16); // val2 : 1B3314
        qDebug() << QByteArray::fromHex(val2); // result : "\x1B\x33\x14"
    
    

    If the Hex value of 'int n' is a single digit,
    I get results that I did not want.
    I want to store the data in bytes.
    How do I solve this problem?

    I thank you in advance for your advice.

    D 1 Reply Last reply
    0
    • K Kycho

      Hello everyone.

      I'm having trouble converting to Hex.
      First is the code.

          /* Error Code */
          int n1 = 1;
          QByteArray val1 = QByteArray("1B33")+QByteArray::number(n1,16); // val1 : 1B331
          qDebug() << QByteArray::fromHex(val1); //result : "\x01\xB3\x31" ( I do not want this value )
          // I want result : "\x1B\x33\x01"
      
          /*Good work Code*/
          int n2 = 20;
          QByteArray val2 = QByteArray("1B33")+QByteArray::number(n2,16); // val2 : 1B3314
          qDebug() << QByteArray::fromHex(val2); // result : "\x1B\x33\x14"
      
      

      If the Hex value of 'int n' is a single digit,
      I get results that I did not want.
      I want to store the data in bytes.
      How do I solve this problem?

      I thank you in advance for your advice.

      D Offline
      D Offline
      Devopia53
      wrote on last edited by
      #2

      @Kycho

      The method varies, but the method by Qt only is something like this.

      QByteArray val1 = QByteArray("1B33")+QByteArray::number(n1,16).rightJustified(2, '0');
      
      1 Reply Last reply
      2

      • Login

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