Results 1 to 7 of 7

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Posts
    105
    Thanks
    13
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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 13:22. Reason: delete note added

Similar Threads

  1. Decimal to Binary conversion
    By anh5kor in forum Newbie
    Replies: 1
    Last Post: 5th February 2015, 12:16
  2. QString to Binary Problem
    By onder_11 in forum Qt Programming
    Replies: 5
    Last Post: 22nd October 2012, 02:43
  3. Convert data in QVector<QString> to Decimal
    By rex in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2011, 00:31
  4. convert decimal seconds to QDateTime and back
    By mcarter in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2010, 12:06
  5. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05

Bookmarks

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.