Convert Hexa to Decimal in Swift

后端 未结 2 2046
傲寒
傲寒 2020-12-22 10:29

I want to convert Hexa value into decimal. So I have tried this. When value is >0, then it is working fine. But when value is <0, then is returns wrong value.

<         


        
2条回答
  •  被撕碎了的回忆
    2020-12-22 10:47

    FF 88 is the hexadecimal representation of the 16-bit signed integer -120. You can create an unsigned integer first and then convert it to the signed counterpart with the same bit pattern:

    let h3 = "FF88"
    let u3 = UInt16(h3, radix: 16)!  // 65416
    let s3 = Int16(bitPattern: u3)   // -120
    

提交回复
热议问题