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 change non-printable characters in QByteArray to '.' (dot)
QtWS25 Last Chance

How to change non-printable characters in QByteArray to '.' (dot)

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.6k 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.
  • T Offline
    T Offline
    TomCrow
    wrote on last edited by
    #1

    I need help with "printing" data of my QByteArray... this is what debug shows what is in the variable:
    !http://s22.postimg.org/7hyh6us8x/debug.png(Debug)!
    I just need to qDebug() the "...............102537*"...everytime when i do something like:
    qDebug() << myByteArray; it shows up some strange symbols... i tried many ways...

    1 Reply Last reply
    0
    • TheBadgerT Offline
      TheBadgerT Offline
      TheBadger
      wrote on last edited by
      #2

      What I do when I need to debug a QByteArray:
      @
      QByteArray myByteArray;
      for(int c = 0; c < myByteArray.size(); ++c) {
      qDebug() << QString("[%1] 0x%2 | %3")
      .arg(c, 2, 10, QChar('0'))
      .arg(myByteArray[c] & 0x00FF, 2, 16, QChar('0'))
      .arg(myByteArray[c] & 0x00FF);
      }
      @

      It prints out each char on its own line, in the following format
      [<CH_IDX>] <HEX_VAL> | <INT_VAL>


      Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        QByteArray holds binary data ... depending on how the data is interpreted there are thousands of ways what the data can actually mean. What you see in the debugger is the text interpretation.

        So the question is, what type of data does your QByteArray actually contain?

        --- 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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TomCrow
          wrote on last edited by
          #4

          Thank you for answers... for working with the qbytearray iam using .toHex() .. but is there any way to save the “……………102537*” to QString... just like text...nothing more? my Qbytearray is from socket.readAll();
          And sorry for my english...

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            [quote author="TomCrow" date="1372245713"]Thank you for answers... for working with the qbytearray iam using .toHex() .. but is there any way to save the “……………102537*” to QString... just like text...nothing more? my Qbytearray is from socket.readAll();
            And sorry for my english...[/quote]

            Yes, but note that, when you write "some thing", it's a encoded C string or bytes stream. You must told QString which charset you used in the C string. Then you can easily convert the C string to proper QString using QString::fromUtf8(), QString::fromLocal8Bit(), or QTextCodec.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TomCrow
              wrote on last edited by
              #6

              mystring = QString::fromUtf8(mybytearray); and fromLocal8Bit() didnt work... i dont need to work with the QString like edit or something...i just need to show it.

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                were does the data of the socket got sent from? from a application of you wrote?

                --- 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

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TomCrow
                  wrote on last edited by
                  #8

                  no, from one small game

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dbzhang800
                    wrote on last edited by
                    #9

                    [quote author="TomCrow" date="1372340596"]mystring = QString::fromUtf8(mybytearray); and fromLocal8Bit() didnt work... i dont need to work with the QString like edit or something...i just need to show it.
                    [/quote]

                    OK, but
                    First, the charset of the bytes stream must be known to you.
                    Then, convert the bytes stream to unicode string.
                    Finially, convert the unicode string to another bytes stream with your system local charset.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TomCrow
                      wrote on last edited by
                      #10

                      Well... the new problem is in printing the non-printable values.. if i do something like that:
                      @string = "26:6f:62:72:61:6e:61";
                      ByteArray = QByteArray::fromHex(string.toLatin1());
                      QString newSTring = ByteArray;@
                      the newString wont show the word "obrana" right because of the non-printable value "26" at the beggining of the QByteArray... how can i convert all non-printable values to "2e"(hex) and then save it to QString?

                      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