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. Convert hexbyte to ASCII
Forum Updated to NodeBB v4.3 + New Features

Convert hexbyte to ASCII

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 276 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by Damian7546
    #1

    Hi,

    Is there any simple way in qt to change 1byte: 0x44 to 2bytes "44" ? I try like below but something is wrong:

    QString MyClass::hexByte2ASCII(quint8 data)
    {
        quint8 data_hi = (data  >> 4);
        quint8 data_lo = ((data & 0x0f));
    
        QString value;
        value[0] = data_hi <= '9' ? data_hi + '0' : data_hi + '7';
        value[1] = data_lo <= '9' ? data_lo + '0' : data_lo + '7';
        return value;
    }
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • D Damian7546

      Hi,

      Is there any simple way in qt to change 1byte: 0x44 to 2bytes "44" ? I try like below but something is wrong:

      QString MyClass::hexByte2ASCII(quint8 data)
      {
          quint8 data_hi = (data  >> 4);
          quint8 data_lo = ((data & 0x0f));
      
          QString value;
          value[0] = data_hi <= '9' ? data_hi + '0' : data_hi + '7';
          value[1] = data_lo <= '9' ? data_lo + '0' : data_lo + '7';
          return value;
      }
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Damian7546 said in Convert hexbyte to ASCII:

      I try like below but something is wrong:

      Then fix it. You're accessing the 'value' QString at an invalid index since the QString is empty. Use QString operator +=(char)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Damian7546
        wrote on last edited by Damian7546
        #3

        @Christian-Ehrlicher thanks for response.
        I found simpler way to achieve my functionality:
        QString value = (QString::number(data, 16)).toUpper();

        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