Results 1 to 5 of 5

Thread: How to convert binary data to hexadecimal data

  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default How to convert binary data to hexadecimal data

    Here I am again but with a different problem.

    I have an object that reads data from the serial port. This is binary data that match some key presses. Each key press has a hexadecimal value that I must extract from the binary data.

    My questions are the following :
    1. Is it possible to convert binary data to hexadecimal data ?
    2. If it is possible, how could I make the convertion between binary to hexadecimal ?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Somewhere in the middle of the State of New York
    Posts
    7
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert binary data to hexadecimal data

    If you are just trying to display Hex Codes you can use the QString::number() Function.

    Qt Code:
    1. int myBase = 16;
    2. QString str = QString::number( my_binary_value, myBase);
    To copy to clipboard, switch view to plain text mode 

    where my_binary_value is one of:
    • int
    • uint
    • Q_LLONG
    • Q_ULLONG
    • long
    • ulong
    • double
    "Power, The only Thing Better than Toast" -- Blitzen The Evil Reindeer

  3. #3
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: How to convert binary data to hexadecimal data

    I used QString::toLong but the conversion always failed.

    I use your code and it is ok.

    thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to convert binary data to hexadecimal data

    Please note, that you can't really distinguish between "binary" and "hexadecimal" data (and "octal" too). All of these have the same representation. Every modern computer keeps its data in all three of those formats at once, so there is no "conversion" from binary to hexadecimal or whatsoever -- there is only a matter of displaying data in binary, octal or hexadecimal form.

    For example, if I want to display a decimal number 100:

    hexadecimal: (100d = 6*16d + 4d) => 64h (0x64)
    octal: (100d = 1*64d + 4*8d + 4d) => 144o (0144)
    binary: (100d = 1*64d + 1*32d + 1*4d) => 1100100b

    All data is stored as a series of bits, with each bit representing a single binary digit. But also three bits form an octal digit and on the same time four bits form a hexadecimal digit. So it is only a matter of taking an appropriate number of bits from the binary encoded values: 1100100 = (001) (100) (100) = (0110) (0100)
    Last edited by wysota; 8th March 2006 at 17:59. Reason: Corrected one mistake

  5. #5
    Join Date
    Jan 2006
    Location
    Somewhere in the middle of the State of New York
    Posts
    7
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert binary data to hexadecimal data

    For those less mathematically inclined a code example of what wysota is saying:

    Qt Code:
    1. #include <iostream>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5.  
    6. int myBase10 = 15; //15 in decimal
    7. int myBase8 = 017; //15 in octal
    8. int myBase16 = 0x000F; //15 in hex (leading zeros just to remind me that this is a 32 bit representation...its a personal preference 0xF would do )
    9.  
    10. if( (myBase10 == myBase16) && (myBase10 == myBase8) && (myBase16 == myBase8) )
    11. {
    12. std::cout << "They are all Equal, Under the Hood the Computer sees each of these as a 32 bit(because typed as int) base2 Number" << std::endl;
    13. std::cout << "eg.( 0000000000001111 )(little endian order listed)" << std::endl;
    14. std::cout << "It is the language and Compiler that give you the convenience of thinking in hex/octal/decimal..." << std::endl;
    15.  
    16. }//end if all ints are equal
    17.  
    18. return( 0 );
    19. }//end main
    To copy to clipboard, switch view to plain text mode 
    Last edited by KMAPSRULE; 8th March 2006 at 16:23.
    "Power, The only Thing Better than Toast" -- Blitzen The Evil Reindeer

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.