I am trying to get a two character hex value from an integer:
let hex = String(format:"%2X", 0)
print ("hex = \(hex)")
hex = "0"
How can I format String to result in always 2 characters, in this case I would want
hex = "00"
You can add a padding 0 before the formatter string:
let hex = String(format:"%02X", 0)
Result:
let hex = String(format:"%02X", 0) // 00
let hex = String(format:"%02X", 15) // 0F
let hex = String(format:"%02X", 16) // 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With