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. QByteArray to QString just like QDebug style
Forum Updated to NodeBB v4.3 + New Features

QByteArray to QString just like QDebug style

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

    I have a QByetArray like this in QDebug():
    "\x01\x02""en\x80\xB1\x04\x0B\x01\x06vovo_euteamo\x00\f2J\f3e\f2l\f1l\f2y \f2C\f3l\f1u\f2b \fJ|\f2S\f1E\fJ| \fEMe\fM0\fEw ..."
    but I need it exactly the same way it is in QDebug(). For instance I need "\x00\f..." to find out that data type has changed. I can't simply iterate in QByteArray. However, things like "toStdString()" doesn't sustain it's qDebug() appearance. I want the description in qDebug() docs:
    "Normally, QDebug prints the array inside quotes and transforms control or non-US-ASCII characters to their C escape sequences (\xAB). This way, the output is always 7-bit clean and the string can be copied from the output and pasted back into C++ sources, if necessary."
    I know I can work with:

    QString::fromStdString( qByteArray.toStdString());
    

    but I'm working with a python script which works with "7-bit clean" just like what qDebug() outputs.
    Is there any way to have a QString with exactly same appearance of qDebug() output from a QByteArray?

    J.HilkJ 1 Reply Last reply
    0
    • SomeoneElseS SomeoneElse

      I have a QByetArray like this in QDebug():
      "\x01\x02""en\x80\xB1\x04\x0B\x01\x06vovo_euteamo\x00\f2J\f3e\f2l\f1l\f2y \f2C\f3l\f1u\f2b \fJ|\f2S\f1E\fJ| \fEMe\fM0\fEw ..."
      but I need it exactly the same way it is in QDebug(). For instance I need "\x00\f..." to find out that data type has changed. I can't simply iterate in QByteArray. However, things like "toStdString()" doesn't sustain it's qDebug() appearance. I want the description in qDebug() docs:
      "Normally, QDebug prints the array inside quotes and transforms control or non-US-ASCII characters to their C escape sequences (\xAB). This way, the output is always 7-bit clean and the string can be copied from the output and pasted back into C++ sources, if necessary."
      I know I can work with:

      QString::fromStdString( qByteArray.toStdString());
      

      but I'm working with a python script which works with "7-bit clean" just like what qDebug() outputs.
      Is there any way to have a QString with exactly same appearance of qDebug() output from a QByteArray?

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

      @SomeoneElse
      hi, I usually go woth something like this, for a user readable display of hex data, this is however in C++ as I'm notusing Phyton :-(

      QString s; QByteArray data;
      for(int i = 0; i < data.size(); i++)
              s.append("0x"+QString::number(static_cast<unsigned char>(data[i]),16).toUpper().rightJustified(2,'0')+ " ");
      
      hope it helps.
      

      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.

      SomeoneElseS 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @SomeoneElse
        hi, I usually go woth something like this, for a user readable display of hex data, this is however in C++ as I'm notusing Phyton :-(

        QString s; QByteArray data;
        for(int i = 0; i < data.size(); i++)
                s.append("0x"+QString::number(static_cast<unsigned char>(data[i]),16).toUpper().rightJustified(2,'0')+ " ");
        
        hope it helps.
        
        SomeoneElseS Offline
        SomeoneElseS Offline
        SomeoneElse
        wrote on last edited by
        #3

        @J.Hilk
        Thanks for you response but the problem is that in qDebug() I have "\f2J". This is only a "J" character. When converting back into number such as hex the "f2" is lost. It only writes "J" as number. While "f2" saves much work of me for another code. In python data seems to be normally read as qDebug() style in Qt. But output of "QString::fromStdString( qByteArray.toStdString());" will be:
        "\x01\x02""en\x80\xB1\x04\x00\x00\n\x00\f2J\f3e\f2l..." is converted into ->
        "\u0001\u0002en??\u0004\u0000\u0000\n\u0000\f2J\f3e\f2l..."
        Here I have those "f"s but something like "\x80\xB1" is converted into "??"! While qDebug() can normally convert into cross language system but I have no idea how to get it. Maybe strange tricks like copy contents of console as they are written!
        What you mentioned is like "toHex" with some spaces among them. With the Hex I can't find out whether it was \x## or \f#(Char), I just have some numbers.

        raven-worxR SomeoneElseS 2 Replies Last reply
        0
        • SomeoneElseS SomeoneElse

          @J.Hilk
          Thanks for you response but the problem is that in qDebug() I have "\f2J". This is only a "J" character. When converting back into number such as hex the "f2" is lost. It only writes "J" as number. While "f2" saves much work of me for another code. In python data seems to be normally read as qDebug() style in Qt. But output of "QString::fromStdString( qByteArray.toStdString());" will be:
          "\x01\x02""en\x80\xB1\x04\x00\x00\n\x00\f2J\f3e\f2l..." is converted into ->
          "\u0001\u0002en??\u0004\u0000\u0000\n\u0000\f2J\f3e\f2l..."
          Here I have those "f"s but something like "\x80\xB1" is converted into "??"! While qDebug() can normally convert into cross language system but I have no idea how to get it. Maybe strange tricks like copy contents of console as they are written!
          What you mentioned is like "toHex" with some spaces among them. With the Hex I can't find out whether it was \x## or \f#(Char), I just have some numbers.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @SomeoneElse
          escape the backslash (\\)

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          SomeoneElseS 1 Reply Last reply
          0
          • SomeoneElseS SomeoneElse

            @J.Hilk
            Thanks for you response but the problem is that in qDebug() I have "\f2J". This is only a "J" character. When converting back into number such as hex the "f2" is lost. It only writes "J" as number. While "f2" saves much work of me for another code. In python data seems to be normally read as qDebug() style in Qt. But output of "QString::fromStdString( qByteArray.toStdString());" will be:
            "\x01\x02""en\x80\xB1\x04\x00\x00\n\x00\f2J\f3e\f2l..." is converted into ->
            "\u0001\u0002en??\u0004\u0000\u0000\n\u0000\f2J\f3e\f2l..."
            Here I have those "f"s but something like "\x80\xB1" is converted into "??"! While qDebug() can normally convert into cross language system but I have no idea how to get it. Maybe strange tricks like copy contents of console as they are written!
            What you mentioned is like "toHex" with some spaces among them. With the Hex I can't find out whether it was \x## or \f#(Char), I just have some numbers.

            SomeoneElseS Offline
            SomeoneElseS Offline
            SomeoneElse
            wrote on last edited by
            #5

            @SomeoneElse
            Sorry, it works. I didn't see that it outputs all characters. That's a good idea. But I wait some days to see if somebody may have an idea to get qDebug() output. Now I can regenerate that string for python.
            Many thanks @J-Hilk

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @SomeoneElse
              escape the backslash (\\)

              SomeoneElseS Offline
              SomeoneElseS Offline
              SomeoneElse
              wrote on last edited by
              #6

              @raven-worx
              Ah sorry maybe I didn't catch that. Those quoted stuff with backslashes are console output, they are not inside code.

              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