Archive 17/01/2023.

[SOLVED] Convert hexadecimal String to unsigned

Mike

I have some trouble converting a hexadecimal String to unsigned.
For example, converting “0xFFFFFF” to 0xFFFFFF or directly to unsigned.
Using String::ToUInt() returns 0.

cadaver

It uses the strtoul function using base 10 so it doesn’t support that notation. Exposing the base as a parameter or adding an overload would work, though.

EDIT: The original reason for that decision (instead of defaulting the base parameter to 0 so it autodetects) is that a string containing beginning zeroes would be autodetected as octals, which is possibly unwanted.

cadaver

Base parameter added in master branch.

Mike

Many thanks for the quick fix, works perfectly :stuck_out_tongue:
Can ToColor() benefit from the same kind of improvement ?

cadaver

We define Color as floats, its string conversion is “r g b a” which is used throughout in Urho’s materials and it’s the format which ToColor() handles.

Handling the common hex encoding of 8-bit RGB colors is outside of what Urho itself requires. It could be added, sure, but I don’t see it especially important.

Mike

OK, it is not mandatory at all, getting the hex is enough.