Explanation of %02.2hhx
in the high vote answer:
%
: Introduces the x
conversion specifier.
02
: The minimum width of the converted value is 2. If the converted value has fewer bytes than the field width, it shall be padded with 0
on the left.
.2
: Gives the minimum number of digits to appear for the x
conversion specifier.
hh
: Specifies that the x
conversion specifier applies to a signed char or unsigned char argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to signed char or unsigned char before printing).
x
: The unsigned argument shall be converted to unsigned hexadecimal format in the style "dddd"; the letters "abcdef" are used. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it shall be expanded with leading zeros. The default precision is 1. The result of converting zero with an explicit precision of zero shall be no characters.
For more details, see the IEEE printf specification.
Based on the above explanation, I think it is better to change %02.2hhx
to %02x
or %.2x
.
For Swift 5, the following methods are all feasible:
deviceToken.map({String(format: "%02x", $0)}).joined()
deviceToken.map({String(format: "%.2x", $0)}).joined()
deviceToken.reduce("", {$0 + String(format: "%02x", $1)})
deviceToken.reduce("", {$0 + String(format: "%.2x", $1)})
The test is as follows:
let deviceToken = (0..<32).reduce(Data(), {$0 + [$1]})
print(deviceToken.reduce("", {$0 + String(format: "%.2x", $1)}))
// Print content:
// 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f