Results 1 to 9 of 9

Thread: How to decode string to human readable format

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2019
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to decode string to human readable format

    I have a binary file. I need to read it and get a QString in human readable format.

    I do like this.

    Qt Code:
    1. QFile * file = new QFile("file1.bin");
    2.  
    3. if(file->open(QIODevice::ReadOnly | QIODevice::Text))
    4. {
    5. QByteArray ba = file->readAll();
    6. qDebug() << ba;
    7. }
    To copy to clipboard, switch view to plain text mode 

    qDebug gives me
    "Z\x04\xFF?7\xC8\x10\x00\x00\x00\x00\x00?\x00\x00\ x00\x00\x00\x00\x00 9K86AT25Y6GV\x03\x00\x00@\x00\x00""00000081UFIJST UJM2A23B0 H2G \x10\x80\x00\x00\x00/\x00@\x00\x02\x00\x02\x07\x00\xFF?\x10\x00?\x00\x1 0\xFC\xFB\x00\x10\x01\xFF\xFF\xFF\x0F\x00\x00\x07\ x00\x03\x00x\x00x\x00x\x00x\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x1F\x00\x06\x17\x00\ x00L\x00@\x00\xF8\x01""B\x00k4\t\x7F""cai0\t\xBE"" ca? \xA0\x00\x00\x00\x80@\xFE\xFF\x00\x00\xFE\xFE\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\xB0\xEA""B%\x0 0\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00P\xE 0\x00\x88""D\xED\xD7\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x1D@\x1C@\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x8A\x00\x00\x00\x00\x00\x00\x 04@\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x 00\x00=\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x1F\x10\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\f\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xA5""A "
    What is this encoding and how can I translate it to readable QString

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to decode string to human readable format

    How should we know what's the internal structure of your file?
    What do you want to display? The hex values of the data? The take a look at QByteArray::toHex()

  3. #3
    Join Date
    May 2019
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to decode string to human readable format

    The whole information is 255 words here. Each word is 2 bytes. The filesize is 511 bytes
    I need to parse it and to find out information in words 27-46

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to decode string to human readable format

    Quote Originally Posted by Mikhail86 View Post
    The whole information is 255 words here. Each word is 2 bytes.
    A "word" in such a context is usually not a string but a number, a two byte integer.

    For parsing such numbers you will need to know which of the two bytes has the higher value.
    This can either be the first (also referred to as Big Endian) or the second (also referred to as Little Endian).

    You then take each byte and put it in the correct position of a large enough integer type, usually by setting the higher byte first, shifting it by 8 bits and then adding the lower byte.

    Cheers,
    _

  5. #5
    Join Date
    May 2019
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to decode string to human readable format

    Quote Originally Posted by anda_skoa View Post
    A "word" in such a context is usually not a string but a number, a two byte integer.

    For parsing such numbers you will need to know which of the two bytes has the higher value.
    This can either be the first (also referred to as Big Endian) or the second (also referred to as Little Endian).

    You then take each byte and put it in the correct position of a large enough integer type, usually by setting the higher byte first, shifting it by 8 bits and then adding the lower byte.

    Cheers,
    _
    And what does Z at the beginning mean?

    Also I can use QByteArray to read byte by byte anyway. But can you explain why the whole size is 511? Perhaps it should be 249?


    Added after 39 minutes:


    Also what does "bits 15:3" for example mean?
    This binary file contains device identification information
    Last edited by Mikhail86; 11th May 2019 at 11:15.

  6. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to decode string to human readable format

    Quote Originally Posted by Mikhail86 View Post
    But can you explain why the whole size is 511? Perhaps it should be 249?
    Again: we don't know what data you want to parse or how big it is.
    Read it into a QByteArray and then you can access the single values with QByteArray::at()

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to decode string to human readable format

    Quote Originally Posted by Mikhail86 View Post
    And what does Z at the beginning mean?
    That obviously depends on the format specification.
    It might not even be a "Z" character, the bits could be part of a larger value.

    Cheers,
    _

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to decode string to human readable format

    This binary file contains device identification information
    So if that is the case, isn't there documentation for this device that describes how to interpret the binary information? Or something from the operating system that documents how information for such devices is written in their device description files?

    The stuff you see when you ask QDebug to print the byte array is the best QDebug can do to translate each byte into a human-readable string. Some of the bytes map onto ASCII characters, like the "Z" at the start. Others cannot be mapped to a printable ASCII character, so QDebug writes them in a hexadecimal form: "\x00" is a byte with the value 0, "\xFF" is a byte with the value 255, for example.

    Some of the information in your file that is shown as longer lengths of continuous ASCII characters are probably the name or id for the device, but without a description of what the binary file is supposed to contain, it is impossible to interpret it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to decode string to human readable format

    Quote Originally Posted by Mikhail86 View Post
    I have a binary file.
    OK.

    I do like this.
    Qt Code:
    1. QFile * file = new QFile("file1.bin");
    2. if(file->open(QIODevice::ReadOnly | QIODevice::Text))
    3. {
    4. QByteArray ba = file->readAll();
    5. qDebug() << ba;
    6. }
    To copy to clipboard, switch view to plain text mode 
    You almost certainly do not want QFile to do line ending translations: depending on platform this may change the data. That is, you do not want QIODevice::Text in the options.

Similar Threads

  1. getting a string value back from long converted format
    By prachi kamble in forum General Programming
    Replies: 0
    Last Post: 1st February 2016, 08:05
  2. QDateTime application level format string
    By davidovv in forum Qt Programming
    Replies: 12
    Last Post: 10th March 2013, 16:53
  3. XML human readable string.
    By m.p in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2011, 14:24
  4. Replies: 5
    Last Post: 19th November 2010, 03:25
  5. Human-Human playable game over Internet
    By A.H.M. Mahfuzur Rahman in forum KDE Forum
    Replies: 0
    Last Post: 2nd August 2009, 04:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.