PDA

View Full Version : QString to Binary Problem

onder_11
1st May 2012, 20:12
Hi everyone;

I'm working on a program which user types a hexadecimal number on a text edit box and now I'm trying to convert it to a binary number.

At last I can get the binary of that number as a QString but in the conversion to integer I have problems..

For example when user types A00A, I can get a QString value binary_res like:

QString binary_res="1010000000001010";
int bin = binary_res.toInt(&ok, 10);

but int bin returns 0.

How can i protect the binary_res as a binary number while conversion or is there any possible ways to
convert hexadecimal number to binary number without using QString ?? :confused:

mvuori
1st May 2012, 20:38
You tell to .toInt that your string represents a base 10 number...
Try this:
int bin = binary_res.toInt(&ok, );

onder_11
1st May 2012, 21:56
I've seen that the problem is about the max value of integer..:) if it's not out of bounds, the conversion with the first code I had written works fine.. Anyway thx mvuori for your answer..

ChrisW67
2nd May 2012, 06:33
the conversion with the first code I had written works fine
No. You tell us your input string is in binary so under no circumstance does treating it as decimal during conversion give you a useful result. You are clearly very confused.

A number is represented inside a computer as a set of binary bits; 32 in four bytes for the average int data type. The thing the user is giving you is a string that represents a number in some base (http://en.wikipedia.org/wiki/Radix); 2 in the case of binary, 10 for decimal, or 16 in the case of hexadecimal. Each character in the string is one or two bytes in the computer's memory, which clearly is a very different beast to a simple int. You can choose to convert that string to a number using the functions in QString, specifically QString::toInt(). To do that you need to tell QString what base the string is in and it will convert. If it cannot convert your result will be zero and the ok flag, if you gave it one, will be false.

QString userInput("A00A"); // this is a hexadecimal string representation of a number, not a number

// So let's try to convert it
bool ok;
int value = userInput.toInt(&ok, 16); // input is a hexadecimal string so this should work
qDebug() << ok << value; // prints: true 40970 (output is in decimal by default)

value = userInput.toInt(&ok, 10); // it is NOT a valid decimal string so this fails
qDebug() << ok << value; // prints: false 0
value = userInput.toInt(&ok, 2); // it is NOT a valid binary string so this fails
qDebug() << ok << value; // prints: false 0

once you have an actual number then you can display it in any human-friendly form you like using QString::number():

int value = userInput.toInt(&ok, 16);
qDebug() << "The number in decimal" << QString::number(value);
qDebug() << "The number in hexadecimal" << QString::number(value, 16);
qDebug() << "The number in binary" << QString::number(value, 2);
qDebug() << "The number in octal" << QString::number(value, 8);

prints:

The number in decimal "40970"
The number in binary "1010000000001010"
The number in octal "120012"

QString::toInt() and QString::number() are in some sense opposite functions.

21st October 2012, 15:16
No. You tell us your input string is in binary so under no circumstance does treating it as decimal during conversion give you a useful result. You are clearly very confused.

A number is represented inside a computer as a set of binary bits; 32 in four bytes for the average int data type. The thing the user is giving you is a string that represents a number in some base (http://en.wikipedia.org/wiki/Radix); 2 in the case of binary, 10 for decimal, or 16 in the case of hexadecimal. Each character in the string is one or two bytes in the computer's memory, which clearly is a very different beast to a simple int. You can choose to convert that string to a number using the functions in QString, specifically QString::toInt(). To do that you need to tell QString what base the string is in and it will convert. If it cannot convert your result will be zero and the ok flag, if you gave it one, will be false.

QString userInput("A00A"); // this is a hexadecimal string representation of a number, not a number

// So let's try to convert it
bool ok;
int value = userInput.toInt(&ok, 16); // input is a hexadecimal string so this should work
qDebug() << ok << value; // prints: true 40970 (output is in decimal by default)

value = userInput.toInt(&ok, 10); // it is NOT a valid decimal string so this fails
qDebug() << ok << value; // prints: false 0
value = userInput.toInt(&ok, 2); // it is NOT a valid binary string so this fails
qDebug() << ok << value; // prints: false 0

once you have an actual number then you can display it in any human-friendly form you like using QString::number():

int value = userInput.toInt(&ok, 16);
qDebug() << "The number in decimal" << QString::number(value);
qDebug() << "The number in hexadecimal" << QString::number(value, 16);
qDebug() << "The number in binary" << QString::number(value, 2);
qDebug() << "The number in octal" << QString::number(value, 8);

prints:

The number in decimal "40970"