PDA

View Full Version : How can i convert hexadecimal to binary in qt?



aurora
6th March 2012, 05:55
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


int FilterColl::BinVal(QString str)
{
bool ok;

int iVal=str.toInt(&ok,16);
if(ok)
{
QString hexnum=QString::number(iVal,16);
std::cout<<"THE HEX VALUE IS:"<<hexnum.toStdString()<<endl;
bool ok1;
iVal=hexnum.toInt(&ok1,2);
if(ok1)
{
cout<<"THE BINARY VALUES IS:"<<iVal<<endl;
return iVal;
}
}
else
{
cout<<"CONVERSION FAILED !!!!!!!!!!!!!"<<endl;
iVal=0;
return (iVal);
}
}

ChrisW67
6th March 2012, 06:22
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?

aurora
6th March 2012, 06:30
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..

ChrisW67
6th March 2012, 06:46
"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?

myta212
6th March 2012, 06:48
Hi,
I am success use this method :


#include <QCoreApplication>
#include <QDebug>
#include <QString>


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
bool ok;

QString str = "FF";
qDebug()<<"Input (HEX) = " << str;

int iVal = str.toInt(&ok,16);
QString binnumber = str.setNum(iVal, 2);


qDebug()<<"Convert to Int = " << QString::number(iVal);
qDebug()<<"Convert to Binary = " << binnumber;

return (0);
}

ChrisW67
6th March 2012, 07:09
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).

aurora
6th March 2012, 07:45
Thank u all...:)
And i wanted to know is there any standard function to perform BITWISE AND operation on such two 'strings of digits'??

myta212
6th March 2012, 08:12
Hi,
I think you must convert your string to integer, so you can process bitwise operation.

Best regards,

Toto

ChrisW67
6th March 2012, 09:21
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:


"123B"
"456F"

and the corresponding binary representation of the same numbers:


"0001001000111011"
"0100010101101111"

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:


int i = 4667; // alternatively int i = 0x123b;
int j = 17775; // alternativley int j = 0x456f;

and the bitwise and:


int and = i & j;
// and == 43;

which can be expressed in hexadecimal or binary strings as:


"2B"
"101011"


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

aurora
12th March 2012, 07:37
Thanks a lot chris for this detailed explanation...:)

oneSwarm
24th March 2012, 03:50
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


int FilterColl::BinVal(QString str)
{
bool ok;

int iVal=str.toInt(&ok,16);
if(ok)
{
QString hexnum=QString::number(iVal,16);
std::cout<<"THE HEX VALUE IS:"<<hexnum.toStdString()<<endl;
bool ok1;
iVal=hexnum.toInt(&ok1,2);
{
cout<<"THE BINARY VALUES IS:"<<iVal<<endl;
return iVal;
}
}
else
{
cout<<"CONVERSION FAILED !!!!!!!!!!!!!"<<endl;
iVal=0;
return (iVal);
}
}



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.