Results 1 to 11 of 11

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

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default How can i convert hexadecimal to binary in qt?

    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. if(ok1)
    13. {
    14. cout<<"THE BINARY VALUES IS:"<<iVal<<endl;
    15. return iVal;
    16. }
    17. }
    18. else
    19. {
    20. cout<<"CONVERSION FAILED !!!!!!!!!!!!!"<<endl;
    21. iVal=0;
    22. return (iVal);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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 can i convert hexadecimal to binary in qt?

    You do realise that "but its not working" is a woefully inadequate description of the problem. Here are the sorts of things that are useful to people you are asking for help: What is not working? What input are you supplying? What output are you receiving? Why is that output a problem? Does the program crash? Which line triggers the crash? What have you tried to find/fix the problem?

    Obvious things:
    • The compiler issues a warning that control can reach the end of a non-void function without returning a value. It always pays to understand why the compiler is warning you about your code. Compilers do not warn for no reason.
    • This is your logic:
      • Convert a string to an int,
      • Convert the int to a hex string again,
      • Try to convert the hex string back to an int by trying to interpret it as a binary string.

      Can you see the problem?
    • Have you bothered to single step through the code in your debugger?
    Last edited by ChrisW67; 6th March 2012 at 06:27.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

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

    Thanks for the reply
    "Not working" implies returns '0' for any hexadecimal value entered....
    I know this method is wrong...thats why i posting here asking for the alternative method in Qt, that others may know..
    Last edited by aurora; 6th March 2012 at 06:35.

  4. #4
    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 can i convert hexadecimal to binary in qt?

    "Not working" implies returns '0' for any hexadecimal value entered....
    As your code stands it can return zero several ways. The output will not be the same for some code paths.

    You have all the methods you need. Your logic is broken in the ways I pointed out in my previous post. The best way to see how your code delivers 0 fro any input is to single-step the code. What code path is followed? What is the value of the variable at each point?

  5. #5
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Hi,
    I am success use this method :
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QString>
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9. bool ok;
    10.  
    11. QString str = "FF";
    12. qDebug()<<"Input (HEX) = " << str;
    13.  
    14. int iVal = str.toInt(&ok,16);
    15. QString binnumber = str.setNum(iVal, 2);
    16.  
    17.  
    18. qDebug()<<"Convert to Int = " << QString::number(iVal);
    19. qDebug()<<"Convert to Binary = " << binnumber;
    20.  
    21. return (0);
    22. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to myta212 for this useful post:

    aurora (6th March 2012)

  7. #6
    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 can i convert hexadecimal to binary in qt?

    Indeed mtya212. Nice string of binary digits

    I am not sure if aurora is actually expecting a string of binary digits (implied by the complexity of the attempted solution), or the actual integer value represented by the input hex string (implied by the return value).

  8. #7
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

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

    Thank u all...
    And i wanted to know is there any standard function to perform BITWISE AND operation on such two 'strings of digits'??

  9. #8
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Hi,
    I think you must convert your string to integer, so you can process bitwise operation.

    Best regards,

    Toto

  10. #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 can i convert hexadecimal to binary in qt?

    Quote Originally Posted by aurora View Post
    And i wanted to know is there any standard function to perform BITWISE AND operation on such two 'strings of digits'??
    No. You appear to be very confused. You do not need a binary string representation of the input hex digits if what you want to do is a bitwise operation between two of them.

    This is a hexadecimal representation of a pair of numbers:
    Qt Code:
    1. "123B"
    2. "456F"
    To copy to clipboard, switch view to plain text mode 
    and the corresponding binary representation of the same numbers:
    Qt Code:
    1. "0001001000111011"
    2. "0100010101101111"
    To copy to clipboard, switch view to plain text mode 
    Note that neither of those things is actually a number from the perspective of doing any sort of mathematics. They are all string literals, i.e. a list of characters.

    These are the integer values that those strings represent:
    Qt Code:
    1. int i = 4667; // alternatively int i = 0x123b;
    2. int j = 17775; // alternativley int j = 0x456f;
    To copy to clipboard, switch view to plain text mode 
    and the bitwise and:
    Qt Code:
    1. int and = i & j;
    2. // and == 43;
    To copy to clipboard, switch view to plain text mode 
    which can be expressed in hexadecimal or binary strings as:
    Qt Code:
    1. "2B"
    2. "101011"
    To copy to clipboard, switch view to plain text mode 

    To go from string to integer use QString::toInt(), and from integer to hex or binary use QString::number().

  11. The following user says thank you to ChrisW67 for this useful post:

    aurora (12th March 2012)

  12. #10
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

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

    Thanks a lot chris for this detailed explanation...

  13. #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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.