I have an 8-character string
representing a hexadecimal number and I need to convert it to an int
. This conversion has to preserve the bit pattern for
While there are ways to do this using casts and conversions, most rely on undefined behavior that happen to have well-defined behaviors on some machines / with some compilers. Instead of relying on undefined behavior, copy the data:
int signed_val;
std::memcpy (signed_val, val, sizeof(int));
return signed_val;