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



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



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


  • Moderators

    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?



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



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


  • Moderators

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



  • no, from one small game



  • [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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.