QString hex translation errors
-
Windows 11, Qt Creator 12.0.2
I was doing some hex to binary translations and came across some errors. This is the code I added while verifying how things work. All is fine for 8 and 16 bits, but there are problems for 32 and 64 bits.
I removed the looping code used for my practics, index variable i is set to zero for the below results.union union_int_256 { uint64_t int_64[ 4 ]; uint32_t int_32[ 8 ]; uint16_t int_16[ 16 ]; uint8_t int_08[ 32 ]; // 64 hex characters }; … QString s08; QString s16; QString s32; QString s64; bool b08; bool b16; bool b32; bool b64; uint8_t x08, z08; uint16_t x16, z16; uint32_t x32, z32; uint64_t x64, z64; x08 = binary->int_08[ i ]; // input: 0XE3 , 227 s08.setNum( x08, 16 ); // s08 = e3 z08 = s08.toInt( &b08, 16 ); // z08 = 227, b08 = true x16 = binary->int_16[ i ]; // input: 0XCDE3, 5207 s16.setNum( x16, 16 ); // s16 = cde3 z16 = s16.toInt( &b16, 16 ); // z16 = 5207, b16 = true x32 = binary->int_32[ i ]; // input: 89ABCDE3, 2309737955 s32.setNum( x32, 16 ); // s32 = 89abcde3 z32 = s32.toInt( &b32, 16 ); // z32 = 0, b32 = false x64 = binary->int_64[ i ]; // input: E2 0123456789ABCDE3, 81985529216486883 s64.setNum( x64, 16 ); // s64 = 123456789abcde3 z64 = s64.toInt( &b64, 16 ); // z64 = 0, b64 = false
and the variable part of the debugger is captured below
Any thoughts?
-
Christian Ehrlicher Lifetime Qt Championreplied to Bryan Kelly on last edited by Christian Ehrlicher
@Bryan-Kelly said in QString hex translation errors:
Any thoughts
Since you don't tell us what you think is wrong nor provide some compileable code with test data - no.
I would guess you want to tell us that toInt() can not convert it back? If so - toInt() converts to a int32 according to the documentation.
-
Hmmm, I though the results were a bit obvious. The status returned for 8 and 16 bits were true, and false for 32 and 64 bits. The hex to int value for 32 bit translation was zero, incorrect. Both the translations for 64 bit were incorrect.
I did not recognize anything in the documentation restricting the translate operation of 8 and 16 bits.My search results did not return the same page that yours did, and were a bit less descriptiive, but still, did not indicate the 8 and 16 bit limitations.
-
So my I guess is correct. Use the correct toFoo() functions if you want everything greater than an int.
-
@Christian-Ehrlicher
I have not tested OP's code and don't have Qt availability. While what you say may be the case for theuint64_t
type, the OP claims it goes wrong for 32-bit (uint32_t
) case as well, which should beint
? -
@JonB said in QString hex translation errors:
it goes wrong for 32-bit (uint32_t ) case as well, which should be int?
u = unsigned
-
@Bryan-Kelly said in QString hex translation errors:
x32 = binary->int_32[ i ]; // input: 89ABCDE3, 2309737955 s32.setNum( x32, 16 ); // s32 = 89abcde3 z32 = s32.toInt( &b32, 16 ); // z32 = 0, b32 = false
In the light of what @Christian-Ehrlicher has just said, can you try this with
0-7
instead of8-F
as the leading hex digit? :) Do ir with e.g.s32 = 79abcde3
, what's the result? And, try the original but use uint QString::toUInt(bool *ok = nullptr, int base = 10) const. The variants for 64-bit useLong
/ULong
.