[Interest] string conversion to longlong error

Calogero Mauceri mauceri at actgate.com
Thu Sep 4 10:35:52 CEST 2014


On 9/3/2014 6:11 PM, Rogers Nate wrote:
> On 09/03/2014 11:53 AM, Calogero Mauceri wrote:
>> Hi all,
>>
>> I'm doing something really simple, but which is driving me crazy.
>>
>> I'm reading a string from a file and converting it to a long long
>> integer, but the conversion fails.
>> The code I'm running is as simple as this
>>
>>       // this string is read from a file
>>       QString str("18446744073709551615");
>>       bool ok;
>>       qint64 value = str.toLongLong(&ok);    // Error
>>
>> the number should be representable in 64 bit. Am I doing something wrong?
>>
>> More hints, if I convert the number to unsigned long long integer then
>> it works
>>
>>        bool ok;
>>        quint64 value = str.toULongLong(&ok);    // OK
>>
>> Similarly initializing the long long from the literal value is failing too
>>
>> Q_INT64_C(18446744073709551615) ;      // Error, returns -1
>> Q_UINT64_C(18446744073709551615);    // OK
>>
>> Thanks for your help!
>>       Calogero
>>
> 18446744073709551615 is too big to fit into a long long.  It is actually the biggest number you can have a unsigned long long.  A long long can't fit the same size number because it has to use one bit to hold the sign.  The maximum number you can fit in a long long is 9223372036854775807.
>

Nate,

thanks for your reply, now I see the problem.

Calogero

-- 
Calogero Mauceri
Software Engineer

Applied Coherent Technology Corporation (ACT)
www.actgate.com





More information about the Interest mailing list