In Obj-C I used to convert an unsigned integer n to a hex string with
在object - c中,我使用一个无符号整数n转换成十六进制字符串
NSString *st = [NSString stringWithFormat:@"%2X", n];
I tried for a long time to translate this into Swift language, but unsuccessfully.
我花了很长时间把它翻译成快速的语言,但没有成功。
5 个解决方案
#1
70
You can now do:
你现在可以做的事:
let n = 14
var st = String(format:"%02X", n)
st += " is the hexadecimal representation of \(n)"
print(st)
0E is the hexadecimal representation of 14
Note: The 2
in this example is the field width and represents the minimum length desired. The 0
tells it to pad the result with leading 0
's if necessary. (Without the 0
, the result would be padded with leading spaces). Of course, if the result is larger than two characters, the field length will not be clipped to a width of 2
; it will expand to whatever length is necessary to display the full result.
注意:本例中的2是字段宽度,表示所需的最小长度。如果需要,0告诉它以0开头的结果填充。(如果没有0,结果将被填充进前导空间)。当然,如果结果大于两个字符,则字段长度不会被裁剪为2的宽度;它将扩展到显示完整结果所需的任何长度。
This only works if you have Foundation
imported (this includes the import of Cocoa
or UIKit
). This isn't a problem if you're doing iOS or macOS programming.
这只适用于导入Foundation(包括Cocoa或UIKit的导入)。如果你在做iOS或macOS编程,这不是问题。
Use uppercase X
if you want A...F
and lowercase x
if you want a...f
:
如果你想要一个…F和小写x,如果你想要…
String(format: "%x %X", 64206, 64206) // "face FACE"
If you want to print integer values larger than UInt32.max
, add ll
(el-el, not eleven) to the format string:
如果您想打印大于UInt32的整数值。max,添加ll (el-el,不是11)到格式字符串:
let n = UInt64.max
print(String(format: "%llX is hexadecimal for \(n)", n))
FFFFFFFFFFFFFFFF is hexadecimal for 18446744073709551615
Original Answer
原来的答案
You can still use NSString
to do this. The format is:
你仍然可以使用NSString来做这个。格式是:
var st = NSString(format:"%2X", n)
This makes st
an NSString
, so then things like +=
do not work. If you want to be able to append to the string with +=
make st
into a String
like this:
这使得st成为一个NSString,因此诸如+=之类的东西不能工作。如果你想在字符串后面加上+= make st,就像这样:
var st = NSString(format:"%2X", n) as String
or
或
var st = String(NSString(format:"%2X", n))
or
或
var st: String = NSString(format:"%2X", n)
Then you can do:
然后你可以做:
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"
#2
43
In Swift there is a specific init
method on String
for exactly this:
在Swift中,字符串上有一个特定的init方法:
let hex = String(0xF, radix: 16, uppercase: false)
println("hex=\(hex)") // Output: f
#3
21
With Swift 3, according to your needs, you may choose one of the three following methods in order to solve your problem.
使用Swift 3,根据您的需要,您可以选择以下三种方法之一来解决您的问题。
#1. Using String
's init(_:radix:uppercase:)
initializer
Swift String
has a init(_:radix:uppercase:)
initializer with the following declaration:
Swift字符串有一个init(_:radix:upper - case:)初始化器,其声明如下:
init<T>(_ value: T, radix: Int = default, uppercase: Bool = default) where T : _SignedInteger
Creates a string representing the given value in base 10, or some other specified base.
创建一个字符串,表示以10为基数的给定值,或其他指定的基数。
The Playground code below shows how to create a String
instance that represents an integer value in hexadecimal format by using init(_:radix:uppercase:)
and without having to import Foundation
:
下面的游戏代码演示了如何使用init(_:radix:大写:)来创建一个表示十六进制格式的整数值的字符串实例,而不需要导入基础:
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. Using String
's init(format:_:)
initializer
Foundation
provides String
a init(format:_:)
initializer. init(format:_:)
has the following declaration:
Foundation提供了一个init(格式:_:)初始化器字符串。init(格式:_:)具有以下声明:
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.返回使用给定格式字符串作为模板初始化的字符串对象,其余参数值将被替换为模板。
The Apple's String Programming Guide gives a list of the format specifiers that are supported by String
and NSString
. Among those format specifiers, %X
has the following description:
苹果的字符串编程指南给出了字符串和NSString支持的格式说明符的列表。在这些格式说明符中,%X有以下描述:
Unsigned 32-bit integer (
unsigned int
), printed in hexadecimal using the digits 0–9 and uppercase A–F.无符号32位整数(无符号整数),使用数字0-9和大写字母A-F以十六进制打印。
The Playground code below shows how to create a String
instance that represents an integer value in hexadecimal format with init(format:_:)
:
下面的操场代码展示了如何创建一个字符串实例,它用init(格式:_:)以十六进制格式表示一个整数值:
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. Using String
's init(format:arguments:)
initializer
Foundation
provides String
a init(format:arguments:)
initializer. init(format:arguments:)
has the following declaration:
Foundation提供了一个init(格式:parameters:)初始化器。init(格式:arguments:)具有以下声明:
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.返回使用给定格式字符串作为模板初始化的字符串对象,其余参数值将根据用户的默认语言环境替换为模板。
The Playground code below shows how to create a String
instance that represents an integer value in hexadecimal format with init(format:arguments:)
:
下面的操场代码展示了如何创建一个字符串实例,它用init(格式:arguments:)以十六进制格式表示一个整数值:
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"
#4
2
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.
在Swift3 import foundation中不需要,至少在项目中不需要。String应该具有NSString的所有功能。
#5
1
Answers above work fine for values in the range of a 32 bit Int, but values over this won't work as the value will roll over.
上面的答案对于32位整型范围内的值是适用的,但是这个范围内的值不会起作用,因为值会滚动。
You need to use the length modifier for values greater than a 32bit Int
您需要使用大于32位整数的值的长度修改器。
%x = Unsigned 32-bit integer (unsigned int)
%x =无符号32位整数(无符号整数)
ll = Length modifiers specifying that a following d, o, u, x, or X conversion specifier applies to a long long or unsigned long long argument.
ll = Length修饰符,指定后面的d、o、u、x或x转换说明符应用于长长长或无符号长参数。
let hexString = String(format:"%llX", decimalValue)
#1
70
You can now do:
你现在可以做的事:
let n = 14
var st = String(format:"%02X", n)
st += " is the hexadecimal representation of \(n)"
print(st)
0E is the hexadecimal representation of 14
Note: The 2
in this example is the field width and represents the minimum length desired. The 0
tells it to pad the result with leading 0
's if necessary. (Without the 0
, the result would be padded with leading spaces). Of course, if the result is larger than two characters, the field length will not be clipped to a width of 2
; it will expand to whatever length is necessary to display the full result.
注意:本例中的2是字段宽度,表示所需的最小长度。如果需要,0告诉它以0开头的结果填充。(如果没有0,结果将被填充进前导空间)。当然,如果结果大于两个字符,则字段长度不会被裁剪为2的宽度;它将扩展到显示完整结果所需的任何长度。
This only works if you have Foundation
imported (this includes the import of Cocoa
or UIKit
). This isn't a problem if you're doing iOS or macOS programming.
这只适用于导入Foundation(包括Cocoa或UIKit的导入)。如果你在做iOS或macOS编程,这不是问题。
Use uppercase X
if you want A...F
and lowercase x
if you want a...f
:
如果你想要一个…F和小写x,如果你想要…
String(format: "%x %X", 64206, 64206) // "face FACE"
If you want to print integer values larger than UInt32.max
, add ll
(el-el, not eleven) to the format string:
如果您想打印大于UInt32的整数值。max,添加ll (el-el,不是11)到格式字符串:
let n = UInt64.max
print(String(format: "%llX is hexadecimal for \(n)", n))
FFFFFFFFFFFFFFFF is hexadecimal for 18446744073709551615
Original Answer
原来的答案
You can still use NSString
to do this. The format is:
你仍然可以使用NSString来做这个。格式是:
var st = NSString(format:"%2X", n)
This makes st
an NSString
, so then things like +=
do not work. If you want to be able to append to the string with +=
make st
into a String
like this:
这使得st成为一个NSString,因此诸如+=之类的东西不能工作。如果你想在字符串后面加上+= make st,就像这样:
var st = NSString(format:"%2X", n) as String
or
或
var st = String(NSString(format:"%2X", n))
or
或
var st: String = NSString(format:"%2X", n)
Then you can do:
然后你可以做:
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"
#2
43
In Swift there is a specific init
method on String
for exactly this:
在Swift中,字符串上有一个特定的init方法:
let hex = String(0xF, radix: 16, uppercase: false)
println("hex=\(hex)") // Output: f
#3
21
With Swift 3, according to your needs, you may choose one of the three following methods in order to solve your problem.
使用Swift 3,根据您的需要,您可以选择以下三种方法之一来解决您的问题。
#1. Using String
's init(_:radix:uppercase:)
initializer
Swift String
has a init(_:radix:uppercase:)
initializer with the following declaration:
Swift字符串有一个init(_:radix:upper - case:)初始化器,其声明如下:
init<T>(_ value: T, radix: Int = default, uppercase: Bool = default) where T : _SignedInteger
Creates a string representing the given value in base 10, or some other specified base.
创建一个字符串,表示以10为基数的给定值,或其他指定的基数。
The Playground code below shows how to create a String
instance that represents an integer value in hexadecimal format by using init(_:radix:uppercase:)
and without having to import Foundation
:
下面的游戏代码演示了如何使用init(_:radix:大写:)来创建一个表示十六进制格式的整数值的字符串实例,而不需要导入基础:
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. Using String
's init(format:_:)
initializer
Foundation
provides String
a init(format:_:)
initializer. init(format:_:)
has the following declaration:
Foundation提供了一个init(格式:_:)初始化器字符串。init(格式:_:)具有以下声明:
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.返回使用给定格式字符串作为模板初始化的字符串对象,其余参数值将被替换为模板。
The Apple's String Programming Guide gives a list of the format specifiers that are supported by String
and NSString
. Among those format specifiers, %X
has the following description:
苹果的字符串编程指南给出了字符串和NSString支持的格式说明符的列表。在这些格式说明符中,%X有以下描述:
Unsigned 32-bit integer (
unsigned int
), printed in hexadecimal using the digits 0–9 and uppercase A–F.无符号32位整数(无符号整数),使用数字0-9和大写字母A-F以十六进制打印。
The Playground code below shows how to create a String
instance that represents an integer value in hexadecimal format with init(format:_:)
:
下面的操场代码展示了如何创建一个字符串实例,它用init(格式:_:)以十六进制格式表示一个整数值:
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. Using String
's init(format:arguments:)
initializer
Foundation
provides String
a init(format:arguments:)
initializer. init(format:arguments:)
has the following declaration:
Foundation提供了一个init(格式:parameters:)初始化器。init(格式:arguments:)具有以下声明:
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.返回使用给定格式字符串作为模板初始化的字符串对象,其余参数值将根据用户的默认语言环境替换为模板。
The Playground code below shows how to create a String
instance that represents an integer value in hexadecimal format with init(format:arguments:)
:
下面的操场代码展示了如何创建一个字符串实例,它用init(格式:arguments:)以十六进制格式表示一个整数值:
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"
#4
2
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.
在Swift3 import foundation中不需要,至少在项目中不需要。String应该具有NSString的所有功能。
#5
1
Answers above work fine for values in the range of a 32 bit Int, but values over this won't work as the value will roll over.
上面的答案对于32位整型范围内的值是适用的,但是这个范围内的值不会起作用,因为值会滚动。
You need to use the length modifier for values greater than a 32bit Int
您需要使用大于32位整数的值的长度修改器。
%x = Unsigned 32-bit integer (unsigned int)
%x =无符号32位整数(无符号整数)
ll = Length modifiers specifying that a following d, o, u, x, or X conversion specifier applies to a long long or unsigned long long argument.
ll = Length修饰符,指定后面的d、o、u、x或x转换说明符应用于长长长或无符号长参数。
let hexString = String(format:"%llX", decimalValue)