Convert Hexa to Decimal in Swift

后端 未结 2 2006
傲寒
傲寒 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 11:11

    Hexadecimal conversion depends on integer Type ( signed , unsigned ) and size ( 64 bits , 32 bits , 16 bits . . ) , this is what you missed .

    source code :

    let h3 = "FF88"
    let d5 = Int16(truncatingBitPattern: strtoul(h3, nil, 16))
    print(d5) // -120
    

提交回复
热议问题