I am trying to get a two character hex value from an integer:
我想从一个整数中获取一个两个字符的十六进制值:
let hex = String(format:"%2X", 0)
print ("hex = \(hex)")
hex = "0"
十六进制=“0”
How can I format String to result in always 2 characters, in this case I would want
如何格式化String以产生总共2个字符,在这种情况下我想要
hex = "00"
十六进制=“00”
1 个解决方案
#1
13
You can add a padding 0 before the formatter string:
您可以在格式化程序字符串之前添加填充0:
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
#1
13
You can add a padding 0 before the formatter string:
您可以在格式化程序字符串之前添加填充0:
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