How to convert an Int to Hex String in Swift

后端 未结 6 1073
我在风中等你
我在风中等你 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:57

    To use

    let string2 = String(format:"%02X", 1)
    print(string2) // prints: "01"
    

    In Swift3 import foundation is not required, At least not in a Project. String should have all the functionality as NSString.

提交回复
热议问题