How to convert an Int to Hex String in Swift
在Obj-C中,我曾使用以下命令将无符号整数n转换为十六进制字符串:
1 | NSString *st = [NSString stringWithFormat:@"%2X", n]; |
我花了很长时间尝试将其翻译成Swift语言,但未成功。
您现在可以执行以下操作:
1 2 3 4 | let n = 14 var st = String(format:"%02X", n) st +=" is the hexadecimal representation of \\(n)" print(st) |
1 0E is the hexadecimal representation of 14
注意:本例中的
这仅在导入了
如果要使用
1 | String(format:"%x %X", 64206, 64206) //"face FACE" |
如果要打印大于
1 2 | let n = UInt64.max print(String(format:"%llX is hexadecimal for \\(n)", n)) |
1 FFFFFFFFFFFFFFFF is hexadecimal for 18446744073709551615
原始答案
您仍然可以使用
1 | var st = NSString(format:"%2X", n) |
这使
1 | var st = NSString(format:"%2X", n) as String |
要么
1 | var st = String(NSString(format:"%2X", n)) |
要么
1 | var st: String = NSString(format:"%2X", n) |
然后,您可以执行以下操作:
1 2 3 4 | let n = 123 var st = NSString(format:"%2X", n) as String st +=" is the hexadecimal representation of \\(n)" //"7B is the hexadecimal representation of 123" |
在Swift中,
1 2 | let hex = String(0xF, radix: 16, uppercase: false) println("hex=\\(hex)") // Output: f |
使用Swift 5,可以根据需要选择以下三种方法之一来解决问题。
#1。使用
Swift
1 | init< T >(_ value: T, radix: Int = 10, uppercase: Bool = false) where T : BinaryInteger |
Creates a string representing the given value in base 10, or some other specified base.
下面的Playground代码显示了如何使用
1 2 3 4 5 6 7 8 | let string1 = String(2, radix: 16) print(string1) // prints:"2" let string2 = String(211, radix: 16) print(string2) // prints:"d3" let string3 = String(211, radix: 16, uppercase: true) print(string3) // prints:"D3" |
#2。使用
1 | init(format: String, _ arguments: CVarArg...) |
Returns a
String object initialized by using a given format string as a template into which the remaining argument values are substituted.
《 Apple的String编程指南》列出了
Unsigned 32-bit integer (
unsigned int ), printed in hexadecimal using the digits 0–9 and uppercase A–F.
下面的Playground代码显示了如何创建
1 2 3 4 5 6 7 8 9 10 11 12 13 | import Foundation let string1 = String(format:"%X", 2) print(string1) // prints:"2" let string2 = String(format:"%02X", 1) print(string2) // prints:"01" let string3 = String(format:"%02X", 211) print(string3) // prints:"D3" let string4 = String(format:"%02X, %02X, %02X", 12, 121, 255) print(string4) // prints:"0C, 79, FF" |
#3。使用
1 | init(format: String, arguments: [CVarArg]) |
Returns a
String object initialized by using a given format string as a template into which the remaining argument values are substituted according to the user’s default locale.
下面的Playground代码显示了如何创建
1 2 3 4 5 6 7 8 9 10 11 12 13 | import Foundation let string1 = String(format:"%X", arguments: [2]) print(string1) // prints:"2" let string2 = String(format:"%02X", arguments: [1]) print(string2) // prints:"01" let string3 = String(format:"%02X", arguments: [211]) print(string3) // prints:"D3" let string4 = String(format:"%02X, %02X, %02X", arguments: [12, 121, 255]) print(string4) // prints:"0C, 79, FF" |
使用
1 2 | let string2 = String(format:"%02X", 1) print(string2) // prints:"01" |
在Swift3中,不需要导入基础,至少在Project中不需要。
String应该具有NSString的所有功能。
以上答案对于32位Int范围内的值均适用,但超过此值的值将无效,因为该值将翻转。
您需要将length修饰符用于大于32bit Int的值
%x = 32位无符号整数(无符号int)
ll =长度修饰符,指定后面的d,o,u,x或X转换说明符适用于long long或无符号long long参数。
1 | let hexString = String(format:"%llX", decimalValue) |
斯威夫特5.2.4
1 2 | let value = 200 let hexString = String(format:"%02X", value) |