Results 1 to 11 of 11

Thread: How can i convert hexadecimal to binary in qt?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Join Date
    Dec 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: How can i convert hexadecimal to binary in qt?

    Quote Originally Posted by aurora View Post
    Hi,
    How can i convert a hexadecimal to binary in qt?
    I am trying as below but its not working....

    I am reading hexadecimal as a string(actually this number resides inside a file, hence reading as a string)
    then converting this string into hexadecimal,
    then converting this to binary...
    the code as shown below...

    this function takes hexadecimal value as string then returns the binary corresponding value
    Qt Code:
    1. int FilterColl::BinVal(QString str)
    2. {
    3. bool ok;
    4.  
    5. int iVal=str.toInt(&ok,16);
    6. if(ok)
    7. {
    8. QString hexnum=QString::number(iVal,16);
    9. std::cout<<"THE HEX VALUE IS:"<<hexnum.toStdString()<<endl;
    10. bool ok1;
    11. iVal=hexnum.toInt(&ok1,2);
    12. {
    13. cout<<"THE BINARY VALUES IS:"<<iVal<<endl;
    14. return iVal;
    15. }
    16. }
    17. else
    18. {
    19. cout<<"CONVERSION FAILED !!!!!!!!!!!!!"<<endl;
    20. iVal=0;
    21. return (iVal);
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 
    something wrong in line 11 iVal=hexnum.toInt(&ok1,2); (hexnum is a string"FF", take a look at QString::toint(&bool,base))
    If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.

    if you add hexnum=QString::number(iVal,2); before it ,then it will performs fine.
    Last edited by oneSwarm; 24th March 2012 at 03:58.

Similar Threads

  1. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:47
  2. How to convert JPEG image into binary data?
    By Gokulnathvc in forum Newbie
    Replies: 1
    Last Post: 7th June 2011, 08:43
  3. Replies: 4
    Last Post: 24th July 2009, 08:48
  4. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

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
  •  
Qt is a trademark of The Qt Company.