ofHexToInt() strange behaviour

Hello everybody,

I’m trying to convert a hexadecimal value to an integer. It seems to behave properly until 3 double digits. After that I receive a 0. Why is it so? is it related to the length of an integer?

string mystr = "0xe99fb3";
cout << " ofHexToInt " << ofHexToInt(mystr) << endl; // will print: ofHexToInt 15310771
mystr = "0xf09f988d";
cout << " ofHexToInt " << ofHexToInt(mystr) << endl; // will print: ofHexToInt 0

I think the problem is the value you try to input is bigger than MAX_INT. An int is coded on 4 bytes (32 bits), but one bit is for the sign, so the max value is something like (2^31 -1) which is 0x7FFF FFFF. The value you trying to put in it is bigger than that so apparently the conversion just fails.

If you need to use values in the full range of 4 bytes you should use unsigned int instead (look at the code in oF and replace the “int” by “unsigned int”

Thank you for the nice explanation. Yes that was the trick. using unsigned int gives no issues…
Do you think ofHexToInt() should warn people about it when receiving 4bytes?
It is strange that OF doesnt throw any message while giving a wrong result…