How does one safely static_cast between unsigned int and int?

后端 未结 6 1280
猫巷女王i
猫巷女王i 2021-02-19 19:36

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

6条回答
  •  终归单人心
    2021-02-19 20:04

    Quoting the C++03 standard, §4.7/3 (Integral Conversions):

    If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

    Because the result is implementation-defined, by definition it is impossible for there to be a truly portable solution.

提交回复
热议问题