View Full Version : How to access QString characters values

12th November 2010, 20:25

I am working in a program and I need to convert a integer to a binary number, and then do different stuff depending on the binary values of that number. My question is, how can I access to the individual characters of a QString binary representation of a number?

My code goes something like this:


int mynumber;

I convert that int to binary using:

QString mybinary(QString::number(mynumber, 2));

Now, how to know the value of each QString character? to do something like this:

if(mybinary[1]==0) other_variable=x;

Can I do even mathematics operations such as?:


I hope you can understand what I mean.

Thank you very much!

12th November 2010, 20:39
QString has operator[] (http://doc.qt.nokia.com/4.6/qstring.html#operator-5b-5d) so the call myQString[0] returns the first QChar from QString, for the calculation part you need to convert the QChar to int (http://doc.qt.nokia.com/4.7/qchar.html#digitValue).

And i think you should tell more about what are you trying to achieve, because this conversions from int to QString, back to int doesn't sound like a good design, maybe you should find another container for your bits, like for example the std::bitset (http://www.sgi.com/tech/stl/bitset.html), or i think Qt got to have something similar.

12th November 2010, 20:57
It looks like what you are needing is bit-mask operations (what Zlatomir was getting at in suggesting std::bitset I think).

12th November 2010, 21:03

I'm not sure.

In my program, I have a Widget with a spinbox on it, that goes from 0 to 255. In that spinbox I input any number. Then, internally, I need to know the binary representation of that number to construct something like a table. Lets say my number is 25, then it is 11001 in binary. Using that binary I have to do something like:

if lsb is 1, then do A
else if lsb is 0, then do B.

if 2nd lsb is 1, then do C.
else if 2nd lsb is 0, then do D.

And so it goes.

Any hints?

Thank you!

13th November 2010, 00:41
Suggested reading:


13th November 2010, 07:04
Just a note that you can't just convert chars and ints.
QString::operator[] might not do what you think it does! Remember that QString holds a unicode string. the [] operator will not return a (correct) number to use in mathematical operations.

So doing:

where mybinary is a string, is not correct.
First convert the char pointed at by mybinary[1] to a correct integer representation that you can use.

13th November 2010, 09:15
For efficiency you can use some bit operators to extract bits from int, but since you have/need just one string and you don't need that many conversions, basically you can do this with just one conversion from that int to a QString, i think it is not really necessary to "play" with bits //unless you are programming for some embedded system with small amount of memory

The code in tbscope post doesn't work, but you don't really need to convert the QChar into int again, you can compare what operator[] returns with the character '0' or '1', something like:

QString str("010100"); //you have the string with 1 and 0
if(str[0] == '0') // you test the first character if it is character '0'
qDebug() << "first character in the string is zero"; //call the function you need

you can do it in a loop, or whatever fit your needs

LE: if you really need the integer back see the link in my first post, also if you don't like the QString approach you can search the Qt documentation for some container for bits (something more or less equivalent for std::bitset) it might be easier to use than bit operators.

15th November 2010, 14:01

I guess bitwise operations in way more elegant and efficient, but considering what I want, I think that comparing the QString with '0' and '1' characters will do the trick.
And I've just tested it and it worked the way I wanted.

Thank you very much!