0

I have the same code in 2 different projects. The Qstring::number(data.toLong(&ok,2),16) works in one project and in the other project it does not work. Does anyone know what the reason could be? The code is as follows

1) unsigned short status;
2) long int setting;
3) bool ok;
4) QString data_selected;
5) data_selected = lineEdit_data->text();  //get the binary value
6) data_selected = QString::number(data_selected.toLong(&ok, 2), 16); //convert binary     value to hex value
7) setting = data_selected.toLong(&ok, 16); //convert string to integer

In line 5 I am getting data from a lineEdit. This line works fine. I just inserted a new text edit box and displayed the data there and there I can see data. I have data as "1000000000001000". Then I execute line 6, the output of which is '8008' in first case and '0' in the other project. This is the problem. The code is exactly the same. I have copied and pasted. But in debugging I can see this difference. Please can anyone tell me why this is happening?

demonplus
  • 5,613
  • 12
  • 49
  • 68
nkp
  • 13
  • 6
  • 2
    zero result means that `toLong` has failed the conversion. It would be good to split line nr 6 to see what `toLong` returns and add check on `ok` variable. In line nr 6 I would use also a `trimmed()` method to strip any white characters (just in case). Add information on Qt version and platform used. – Marek R Jan 31 '14 at 16:30
  • 1
    what is the original value of data_selected? Also check the value of `ok` after the conversion. – Frank Osterfeld Jan 31 '14 at 17:13
  • @ Marek : Should this be the command? QString::number(data_selected.trimmed().toLong(&ok, 2), 16); – nkp Jan 31 '14 at 20:11
  • @ Frank : data_selected = "1000000000001000" – nkp Jan 31 '14 at 20:12

1 Answers1

0

I thought that comment under answer was clear.
Correct code like this to detect what is the problem:

ulong setting;
bool ok;
data_selected = data_selected.trimmed(); // first try without this line
ulong value = data_selected.toULong(&ok, 2);
if (ok) {
    data_selected = QString::number(value, 16);
    setting = data_selected.toULong(&ok, 16);
} else {
    data_selected = "convertion error";
}
Marek R
  • 32,568
  • 6
  • 55
  • 140