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; 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
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
To copy to clipboard, switch view to plain text mode
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);
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);
To copy to clipboard, switch view to plain text mode
prints:
The number in decimal "40970"
The number in hexadecimal "a00a"
The number in binary "1010000000001010"
The number in octal "120012"
The number in decimal "40970"
The number in hexadecimal "a00a"
The number in binary "1010000000001010"
The number in octal "120012"
To copy to clipboard, switch view to plain text mode
QString::toInt() and
QString::number() are in some sense opposite functions.
Bookmarks