In Obj-C I used to convert an unsigned integer n to a hex string with
NSString *st = [NSString stringWithFormat:@"%2X", n];
I tried for a long time to translate this into Swift language, but unsuccessfully.
In Obj-C I used to convert an unsigned integer n to a hex string with
NSString *st = [NSString stringWithFormat:@"%2X", n];
I tried for a long time to translate this into Swift language, but unsuccessfully.
In Swift there is a specific
init
method onString
for exactly this:To use
In Swift3 import foundation is not required, At least not in a Project. String should have all the functionality as NSString.
With Swift 5, according to your needs, you may choose one of the three following methods in order to solve your problem.
#1. Using
String
'sinit(_:radix:uppercase:)
initializerSwift
String
has ainit(_:radix:uppercase:)
initializer with the following declaration:The Playground code below shows how to create a
String
instance that represents an integer value in hexadecimal format by usinginit(_:radix:uppercase:)
and without having to importFoundation
:#2. Using
String
'sinit(format:_:)
initializerFoundation
providesString
ainit(format:_:)
initializer.init(format:_:)
has the following declaration:The Apple's String Programming Guide gives a list of the format specifiers that are supported by
String
andNSString
. Among those format specifiers,%X
has the following description:The Playground code below shows how to create a
String
instance that represents an integer value in hexadecimal format withinit(format:_:)
:#3. Using
String
'sinit(format:arguments:)
initializerFoundation
providesString
ainit(format:arguments:)
initializer.init(format:arguments:)
has the following declaration:The Playground code below shows how to create a
String
instance that represents an integer value in hexadecimal format withinit(format:arguments:)
: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.
You need to use the length modifier for values greater than a 32bit Int
%x = Unsigned 32-bit integer (unsigned int)
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.
You can now do:
Note: The
2
in this example is the field width and represents the minimum length desired. The0
tells it to pad the result with leading0
's if necessary. (Without the0
, 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 of2
; it will expand to whatever length is necessary to display the full result.This only works if you have
Foundation
imported (this includes the import ofCocoa
orUIKit
). This isn't a problem if you're doing iOS or macOS programming.Use uppercase
X
if you wantA...F
and lowercasex
if you wanta...f
:If you want to print integer values larger than
UInt32.max
, addll
(el-el, not eleven) to the format string:Original Answer
You can still use
NSString
to do this. The format is:This makes
st
anNSString
, so then things like+=
do not work. If you want to be able to append to the string with+=
makest
into aString
like this:or
or
Then you can do: