[英]Swift Convert Integer to 2 character Hex String
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”
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.