How to convert an Int to Hex String in Swift

后端 未结 6 1076
我在风中等你
我在风中等你 2020-11-29 23:59

In Obj-C I used to convert an unsigned integer n to a hex string with

 NSString *st = [NSString stringWithFormat:@\"%2X\", n];

I tried for

6条回答
  •  野性不改
    2020-11-30 00:59

    In Swift there is a specific init method on String for exactly this:

    let hex = String(0xF, radix: 16, uppercase: false)
    println("hex=\(hex)") // Output: f
    

提交回复
热议问题