How does one safely static_cast between unsigned int and int?

后端 未结 6 1274
猫巷女王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:12

    unsigned int u = ~0U;
    int s = *reinterpret_cast(&u); // -1
    

    Сontrariwise:

    int s = -1;
    unsigned int u = *reinterpret_cast(&s); // all ones
    

提交回复
热议问题