jimbo
29th September 2015, 13:14
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.
QString hex, bReturn, tmpReturn;
QList<quint64> uintList = QList<quint64>();
int split = 32; // 1, 2, 4, 8, 16, 32 byte data split
QString data = readEeprom(eepromAdd, startAdd, bytesToRead, i2c);
int dl = data.length();
if (dl %split != 0) {
qDebug() << "Data length is not wholly divisible by split - (" << split << ")";
return; //exit(1);
}
qDebug() << "data - " << data << "-" << dl << "bytes -" << "split =" << split << endl;
int count = 0;
for (int i = 0; i < dl; i += split) {
hex = data.mid(i, split);
qDebug() << "hex1 - " << hex;
qDebug() << "dec1 - " << hex.toULongLong(&ok, 16); // *** this is wrong on 32 byte splits
tmpReturn = bReturn = hexToBinary(hex);
uintList.append(hex.toULongLong(&ok, 16)); //some sort of error handling on &ok // ***suspect error here***
qDebug() << count + 1 << "-" << "split" << split << uintList << "*** decimal" << uintList[count]
<< "- hexToBinary" << bReturn
<< "- binaryToHex" << binaryToHex(tmpReturn.remove(" "))
//<< "- binaryToDec" << binaryToDec(bReturn.remove(" ").rightJustified(split, '0'))
//<< "- decToBinary" << decToBinary(uintList[count]).rightJustified(split * 4, '0'); //pad with leading zeros
;
QString myProg::binaryToHex(QString sBin) { // eg. sBin = "00000000000011110000000000010000"
qDebug() << "sbin1 - " << sBin; //ok
qDebug() << "sbin2 - " << sBin.remove(QRegExp("^[0]*")); //ok
const std::string bin = sBin.remove(QRegExp("^[0]*")).toStdString();
quint64 result = 0;
for(size_t count = 0; count < bin.length(); ++count) {
result *=2;
result += bin[count]=='1'? 1 :0;
}
qDebug() << "len - " << bin.length(); //ok
qDebug() << "res - " << result; //ok
QString res = QString::number(result, 16);
return (res);
}
data - "000100020003000400050006000700080009000a000b000c00 0d000e000f0010" - 64 bytes - split = ??
split = 8
hex1 - "000f0010"
dec1 - 983056
sbin1 - "00000000000011110000000000010000"
sbin2 - "11110000000000010000"
len - 20
res - 983056
8 - split 8 (65538, 196612, 327686, 458760, 589834, 720908, 851982, 983056) *** decimal 983056 - hexToBinary "0000 0000 0000 1111 0000 0000 0001 0000" - binaryToHex "f0010"
split = 16
hex1 - "000d000e000f0010"
dec1 - 3659234827763728
sbin1 - "00000000000011010000000000001110000000000000111100 00000000010000"
sbin2 - "11010000000000001110000000000000111100000000000100 00"
len - 52
res - 3659234827763728
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"
split = 32
hex1 - "0009000a000b000c000d000e000f0010"
dec1 - 0
sbin1 - "00000000000010010000000000001010000000000000101100 00000000001100000000000000110100000000000011100000 0000000011110000000000010000"
sbin2 - "10010000000000001010000000000000101100000000000011 00000000000000110100000000000011100000000000001111 0000000000010000"
len - 116
res - 3659234827763728 *** should be 5188234733137453056 - max value quint64 18446744073709551615***
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"
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.
QString hex, bReturn, tmpReturn;
QList<quint64> uintList = QList<quint64>();
int split = 32; // 1, 2, 4, 8, 16, 32 byte data split
QString data = readEeprom(eepromAdd, startAdd, bytesToRead, i2c);
int dl = data.length();
if (dl %split != 0) {
qDebug() << "Data length is not wholly divisible by split - (" << split << ")";
return; //exit(1);
}
qDebug() << "data - " << data << "-" << dl << "bytes -" << "split =" << split << endl;
int count = 0;
for (int i = 0; i < dl; i += split) {
hex = data.mid(i, split);
qDebug() << "hex1 - " << hex;
qDebug() << "dec1 - " << hex.toULongLong(&ok, 16); // *** this is wrong on 32 byte splits
tmpReturn = bReturn = hexToBinary(hex);
uintList.append(hex.toULongLong(&ok, 16)); //some sort of error handling on &ok // ***suspect error here***
qDebug() << count + 1 << "-" << "split" << split << uintList << "*** decimal" << uintList[count]
<< "- hexToBinary" << bReturn
<< "- binaryToHex" << binaryToHex(tmpReturn.remove(" "))
//<< "- binaryToDec" << binaryToDec(bReturn.remove(" ").rightJustified(split, '0'))
//<< "- decToBinary" << decToBinary(uintList[count]).rightJustified(split * 4, '0'); //pad with leading zeros
;
QString myProg::binaryToHex(QString sBin) { // eg. sBin = "00000000000011110000000000010000"
qDebug() << "sbin1 - " << sBin; //ok
qDebug() << "sbin2 - " << sBin.remove(QRegExp("^[0]*")); //ok
const std::string bin = sBin.remove(QRegExp("^[0]*")).toStdString();
quint64 result = 0;
for(size_t count = 0; count < bin.length(); ++count) {
result *=2;
result += bin[count]=='1'? 1 :0;
}
qDebug() << "len - " << bin.length(); //ok
qDebug() << "res - " << result; //ok
QString res = QString::number(result, 16);
return (res);
}
data - "000100020003000400050006000700080009000a000b000c00 0d000e000f0010" - 64 bytes - split = ??
split = 8
hex1 - "000f0010"
dec1 - 983056
sbin1 - "00000000000011110000000000010000"
sbin2 - "11110000000000010000"
len - 20
res - 983056
8 - split 8 (65538, 196612, 327686, 458760, 589834, 720908, 851982, 983056) *** decimal 983056 - hexToBinary "0000 0000 0000 1111 0000 0000 0001 0000" - binaryToHex "f0010"
split = 16
hex1 - "000d000e000f0010"
dec1 - 3659234827763728
sbin1 - "00000000000011010000000000001110000000000000111100 00000000010000"
sbin2 - "11010000000000001110000000000000111100000000000100 00"
len - 52
res - 3659234827763728
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"
split = 32
hex1 - "0009000a000b000c000d000e000f0010"
dec1 - 0
sbin1 - "00000000000010010000000000001010000000000000101100 00000000001100000000000000110100000000000011100000 0000000011110000000000010000"
sbin2 - "10010000000000001010000000000000101100000000000011 00000000000000110100000000000011100000000000001111 0000000000010000"
len - 116
res - 3659234827763728 *** should be 5188234733137453056 - max value quint64 18446744073709551615***
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"