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 output all QByteArray data in hex with qDebug()?
Forum Updated to NodeBB v4.3 + New Features

How to output all QByteArray data in hex with qDebug()?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 487 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.
  • bigguinessB Offline
    bigguinessB Offline
    bigguiness
    wrote on last edited by
    #1

    Hello,

    I'm trying to debug some code using QByteArray. When I try to output the data with qDebug() I get something like this:

    code:
        QByteArray ba.resize(5);
        ba[0] = 0x02;
        ba[1] = 0x00;
        ba[2] = 0x07;
        ba[3] = 0x61;
        ba[4] = 0x01;
        qDebug() << ba;
    
    output:
    "\x02\x00\x07"a"\x01"
    

    Is there anyway to make the qDebug() output show the ASCII characters in hex also? I was expecting the output to look like:

    output:
    "\x02\x00\x07\x61\x01"
    

    Thanks

    Christian EhrlicherC 1 Reply Last reply
    0
    • bigguinessB bigguiness

      Hello,

      I'm trying to debug some code using QByteArray. When I try to output the data with qDebug() I get something like this:

      code:
          QByteArray ba.resize(5);
          ba[0] = 0x02;
          ba[1] = 0x00;
          ba[2] = 0x07;
          ba[3] = 0x61;
          ba[4] = 0x01;
          qDebug() << ba;
      
      output:
      "\x02\x00\x07"a"\x01"
      

      Is there anyway to make the qDebug() output show the ASCII characters in hex also? I was expecting the output to look like:

      output:
      "\x02\x00\x07\x61\x01"
      

      Thanks

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @bigguiness said in How to output all QByteArray data in hex with qDebug()?:

      Is there anyway to make the qDebug()

      I can find something at https://doc.qt.io/qt-6/qtextstream.html#qtextstream-manipulators so you have to convert it by yourself.

      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
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        Yes as @Christian-Ehrlicher says you'll have to add some code sprinkles yourself, say you do the qDebug() line like this:

        qDebug().noquote() << "\\x" + ba.toHex(' ').replace(" ","\\x");
        
        bigguinessB 1 Reply Last reply
        4
        • hskoglundH hskoglund

          Yes as @Christian-Ehrlicher says you'll have to add some code sprinkles yourself, say you do the qDebug() line like this:

          qDebug().noquote() << "\\x" + ba.toHex(' ').replace(" ","\\x");
          
          bigguinessB Offline
          bigguinessB Offline
          bigguiness
          wrote on last edited by
          #4

          @hskoglund Thanks!

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5
            std::ostirngstream out;
            out << hex << 0x01U << 0x02U << 0x03U;
            qDebug << out.str().c_str();
            

            use ostringtream since it will take stream conversion manipulators, and then output the str() value to qDebug

            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