You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, May 1st 2012, 4:16pm

QString to Binary problem

Hi everyone;

I'm working on a program which user types a hexadecimal number on a text edit box and 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 ?? ?(


Thanks in advance..

2

Tuesday, May 1st 2012, 6:52pm

int bin = binary_res.toInt(&ok, 2);
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Tuesday, May 1st 2012, 6:58pm

It didn't work anyway.. When I enter "A00A" with this change, it returns the decimal value 40970 , not the binary.. :thumbdown:

4

Tuesday, May 1st 2012, 7:28pm

didnt work?

dont know what you mean.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <QtCore/QCoreApplication>

#include <QString>

#include <QDebug>

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

    QString seven = "111";

    bool b;
    int x = seven.toInt(&b, 2);

    qDebug() << b << x;
    return a.exec();
}

Quoted


true 7


That is correct behaviour. If you want int to mean something that isnt int, then, well, thats your problem :p

Please state what is your input, and what is your desired output.

From your op you have input as 'hex string'. You state you can convert that into binary string already, so you dont want that. You then seem to be trying to convert the binary string into a number. Well, you can convert the hex string into the same number without converting it to binary string first. I played along anyway, and showed you how to get the int from a 'base two string val' (int is int, not int base 10, or int base 2). But that is not right, apparently.

So what do you actually want to do?


NB
in c++
int w = 1.1e1; x = 0xb; int y = 013; int z = 11;

all have the same value: 11. If you want 11 to look like 1011, then it is just a matter of representation and you are back to changing a number into a string.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 2 times, last edit by "Amleto" (May 1st 2012, 7:43pm)


5

Tuesday, May 1st 2012, 9:03pm

Yes I only wanted to protect the binary version of the string while converting it to integer.. And user types only hexadecimal inputs.. So:

If input is 11 (hexadecimal)
It means 00010001 in binary..

Anyway I found out that the problem is only about bounds of integer.. :whistling: when it's not out of bounds, my first code returns true value like:

Input:AA
Output:10101010

Thx Amleto for your answers.. :)