# Thread: Problem with number types - convert binary QString to decimal

1. Intermediate user
Join Date
Oct 2006
Posts
105
Thanks
13
Thanked 4 Times in 4 Posts
Qt products
Platforms

## Problem with number types - convert binary QString to decimal

linux - Qt 4.8.2

Hello,

I'm trying to convert binary strings to decimal.
Getting an error only on 32 bit binary strings. I've marked where I think the error could be, but not shore.
1, 2, 4, 8, & 16 splits are fine.
Perhaps some kind soul can explain where I'm going wrong.

Regards

Maybe thia question does not belong on this forum, feel free to delete.
Qt Code:
1. QString hex, bReturn, tmpReturn;
2. QList<quint64> uintList = QList<quint64>();
3. int split = 32; // 1, 2, 4, 8, 16, 32 byte data split
4.
5. QString data = readEeprom(eepromAdd, startAdd, bytesToRead, i2c);
6.
7. int dl = data.length();
8. if (dl %split != 0) {
9. qDebug() << "Data length is not wholly divisible by split - (" << split << ")";
10. return; //exit(1);
11. }
12.
13. qDebug() << "data - " << data << "-" << dl << "bytes -" << "split =" << split << endl;
14.
15. int count = 0;
16. for (int i = 0; i < dl; i += split) {
17. hex = data.mid(i, split);
18. qDebug() << "hex1 - " << hex;
19. qDebug() << "dec1 - " << hex.toULongLong(&ok, 16); // *** this is wrong on 32 byte splits
20. tmpReturn = bReturn = hexToBinary(hex);
21. uintList.append(hex.toULongLong(&ok, 16)); //some sort of error handling on &ok // ***suspect error here***
22.
23. qDebug() << count + 1 << "-" << "split" << split << uintList << "*** decimal" << uintList[count]
24. << "- hexToBinary" << bReturn
25. << "- binaryToHex" << binaryToHex(tmpReturn.remove(" "))
26. //<< "- binaryToDec" << binaryToDec(bReturn.remove(" ").rightJustified(split, '0'))
27. //<< "- decToBinary" << decToBinary(uintList[count]).rightJustified(split * 4, '0'); //pad with leading zeros
28. ;
To copy to clipboard, switch view to plain text mode

Qt Code:
1. QString myProg::binaryToHex(QString sBin) { // eg. sBin = "00000000000011110000000000010000"
2. qDebug() << "sbin1 - " << sBin; //ok
3. qDebug() << "sbin2 - " << sBin.remove(QRegExp("^[0]*")); //ok
4.
5. const std::string bin = sBin.remove(QRegExp("^[0]*")).toStdString();
6. quint64 result = 0;
7.
8. for(size_t count = 0; count < bin.length(); ++count) {
9. result *=2;
10. result += bin[count]=='1'? 1 :0;
11. }
12. qDebug() << "len - " << bin.length(); //ok
13. qDebug() << "res - " << result; //ok
14. QString res = QString::number(result, 16);
15.
16. return (res);
17. }
To copy to clipboard, switch view to plain text mode
Qt Code:
1. data - "000100020003000400050006000700080009000a000b000c000d000e000f0010" - 64 bytes - split = ??
2.
3. split = 8
4. hex1 - "000f0010"
5. dec1 - 983056
6. sbin1 - "00000000000011110000000000010000"
7. sbin2 - "11110000000000010000"
8. len - 20
9. res - 983056
10. 8 - split 8 (65538, 196612, 327686, 458760, 589834, 720908, 851982, 983056) *** decimal 983056 - hexToBinary "0000 0000 0000 1111 0000 0000 0001 0000" - binaryToHex "f0010"
11.
12. split = 16
13. hex1 - "000d000e000f0010"
14. dec1 - 3659234827763728
15. sbin1 - "0000000000001101000000000000111000000000000011110000000000010000"
16. sbin2 - "1101000000000000111000000000000011110000000000010000"
17. len - 52
18. res - 3659234827763728
19. 4 - split 16 (281483566841860, 1407400653815816, 2533317740789772, 3659234827763728) *** decimal 3659234827763728 - hexToBinary "0000 0000 0000 1101 0000 0000 0000 1110 0000 0000 0000 1111 0000 0000 0001 0000" - binaryToHex "d000e000f0010"
20.
21. split = 32
22. hex1 - "0009000a000b000c000d000e000f0010"
23. dec1 - 0
24. sbin1 - "00000000000010010000000000001010000000000000101100000000000011000000000000001101000000000000111000000000000011110000000000010000"
25. sbin2 - "10010000000000001010000000000000101100000000000011000000000000001101000000000000111000000000000011110000000000010000"
26. len - 116
27. res - 3659234827763728 *** should be 5188234733137453056 - max value quint64 18446744073709551615***
28. 2 - split 32 (0, 0) *** decimal 0 - hexToBinary "0000 0000 0000 1001 0000 0000 0000 1010 0000 0000 0000 1011 0000 0000 0000 1100 0000 0000 0000 1101 0000 0000 0000 1110 0000 0000 0000 1111 0000 0000 0001 0000" - binaryToHex "d000e000f0010"
To copy to clipboard, switch view to plain text mode
Last edited by jimbo; 29th September 2015 at 14:22. Reason: delete note added

2. ## Re: Problem with number types - convert binary QString to decimal

My approach to problems like this is to use a reference to the type you want to convert the data to

Example
Qt Code:
1. // assuming data is 32 bit integer
2. int Conversion::Integer(
3. const unsigned int index,
4. const QByteArray &array)
5. {
6. int i;
7. char *c;
8. unsigned int cntr;
9.
10. assert((index + sizeof(int) -1) < static_cast<unsigned int>(array.size()));
11.
12. c = (char*)(&i);
13.
14. for(cntr = 0;cntr < sizeof(int);cntr++)
15. {
16. *(c + cntr) = array[index + cntr];
17. }
18.
19. return i;
20. }
To copy to clipboard, switch view to plain text mode
In my example I am interested in converting part of the input data into a 32 bit integer. The input 'index' value is the offset from where to start the data conversion in the input array data. The pointer character 'c' is a reference to the same address (stack) space as the returned integer 'i'. When you modify the text contents of 'c' you are also changing the binary data of 'i'.

QByteArray has methods to convert to and from hexadecimal. Your data (i.e. "000100020003000400050006000700080009000a000b000c0 00d000e000f0010") is in hexadecimal. If you want to extract integer, floating point values, text, or whatever from your data you need to convert it from hex first. Prior to calling the above example you must call QByteArray::fromHex(...) to convert "000100020003000400050006000700080009000a000b000c0 00d000e000f0010" to actual binary data.

3. Advanced user
Join Date
Dec 2009
Location
New Orleans, Louisiana
Posts
791
Thanks
13
Thanked 153 Times in 150 Posts
Qt products
Platforms

## Re: Problem with number types - convert binary QString to decimal

Originally Posted by jimbo
I'm trying to convert binary strings to decimal.
I am not really sure I understand what you're trying to accomplish. You state you want to convert to decimal, yet you appear to want to convert to the hex equivalent of your binary string. Is there a reason you can't do something as simple as:
Qt Code:
1. book ok;
2. QString binary = "00000000000011110000000000010000";
3. qint64 dec = binary.toLongLong(&ok, 2);
4. QString hex;
5. hex.sprintf("%llx", dec);
6. qDebug() << "bin=" << binary;
7. qDebug() << "dec=" << dec;
8. qDebug() << "hex=" << hex;
To copy to clipboard, switch view to plain text mode

4. Intermediate user
Join Date
Oct 2006
Posts
105
Thanks
13
Thanked 4 Times in 4 Posts
Qt products
Platforms

## Re: Problem with number types - convert binary QString to decimal

Hello jefftee,

Thanks for your example, its a lot shorter than mine. (my ignorance)
I think I should have been more explicit with my problem.
Splits at 1, 2, 4, 8 and 16 give corrrect results.
The problem is with the 32 bit split, decimal and hex return "0" with your code and mine.
I think its probably a problem with overflow and number types. (don't know)

Regards

Qt Code:
1. qDebug() << "data - " << data << "-" << dl << "bytes -" << "split =" << split << endl;
2.
3. for (int i = 0; i < dl; i += split) {
4. hex = data.mid(i, split);
5. tmpReturn = hexToBinary(hex); //my hexToBinary() tested and works.
6. qDebug() << "hex 1 - " << hex;
7. qDebug() << "bin - " << tmpReturn;
8.
9. bool ok;
10. QString binary = tmpReturn.remove( " ");
11. qint64 dec1 = binary.toLongLong(&ok, 2);
12. if (!ok) qDebug() << "error";
13. QString hex;
14. hex.sprintf("%llx", dec1);
15. qDebug() << "bin2 = " << binary;
16. qDebug() << "dec2 = " << dec1;
17. qDebug() << "hex2 = " << hex;
18. qDebug() << "";
19. }
To copy to clipboard, switch view to plain text mode
Qt Code:
1. //Output for a 16 bit split
2.
3. data - "000100020003000400050006000700080009000a000b000c000d000e000f0010" - 64 bytes - split = 16
4.
5. //Loops 0, 1, and 2 omittted.
6.
7. hex 1 - "000d000e000f0010"
8. bin - "0000 0000 0000 1101 0000 0000 0000 1110 0000 0000 0000 1111 0000 0000 0001 0000"
9. bin2 = "0000000000001101000000000000111000000000000011110000000000010000"
10. dec2 = 3659234827763728
11. hex2 = d000e000f0010"
To copy to clipboard, switch view to plain text mode
Qt Code:
1. //Output for a 32bit split
2.
3. data - "000100020003000400050006000700080009000a000b000c000d000e000f0010" - 64 bytes - split = 32
4.
5. //Loop 0 omittted.
6.
7. hex 1 - "0009000a000b000c000d000e000f0010"
8. bin - "0000 0000 0000 1001 0000 0000 0000 1010 0000 0000 0000 1011 0000 0000 0000 1100 0000 0000 0000 1101 0000 0000 0000 1110 0000 0000 0000 1111 0000 0000 0001 0000"
9. error
10. bin2 = "00000000000010010000000000001010000000000000101100000000000011000000000000001101000000000000111000000000000011110000000000010000"
11. dec2 = 0
12. hex2 = "0"
To copy to clipboard, switch view to plain text mode
Last edited by jimbo; 30th September 2015 at 14:29. Reason: added error handling

5. Advanced user
Join Date
Dec 2009
Location
New Orleans, Louisiana
Posts
791
Thanks
13
Thanked 153 Times in 150 Posts
Qt products
Platforms

## Re: Problem with number types - convert binary QString to decimal

Integer overflow is certainly your issue. See the data below for min/max and sizes for qlonglong:

qlonglong size (bytes) = 8
qlonglong bits = 64 (8 bits/byte times 8 bytes = 64 bits with 63 for precision 1 for sign)
qlonglong min = -9,223,372,036,854,775,808
qlonglong max = +9,223,372,036,854,775,807

What you refer to as a 16-bit split actually requires 64 bits or 8 byte precision, which does not overflow a qlonglong. i.e. You are taking 16 positions from the string, each requires 4 bits to represent the range of possible 0-F values, for a total of 16 x 4 = 64 bits.

What you are calling a 32-bit split would actually require an integer data type with 128 bit or 16 byte precision, which will overflow a qlonglong or qulonglong. i.e. You are taking 32 positions, each requires 4 bits to represent the range of possible 0-F values, for a total of 32 x 4 = 128 bits or 16 bytes.

So, your quest for a 32-bit split [sic] is not possible, nor do I see why you would even care to do so. Since your code works for "splits" up to 16 positions (or the more simplified code I provided also works), is/was this just some educational exercise or why do you perceive the need to process in 32 position chunks?

6. Intermediate user
Join Date
Oct 2006
Posts
105
Thanks
13
Thanked 4 Times in 4 Posts
Qt products
Platforms

## Re: Problem with number types - convert binary QString to decimal

Hello jefftee,

is/was this just some educational exercise
Yes, I'm afraid so! While I'm trying to learn.
Thank you for your time and effort.

Regards

7. Advanced user
Join Date
Dec 2009
Location
New Orleans, Louisiana
Posts
791
Thanks
13
Thanked 153 Times in 150 Posts
Qt products
Platforms

## Re: Problem with number types - convert binary QString to decimal

Heh, not a problem, glad I could help. I just wanted to make sure there wasn't something I was missing. Believe me, I've been down many rabbit holes myself trying to understand why something worked or didn't work accordingly, so I completely understand!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.