2

After searching for a while, I can't figure out how to get this simple result:

let byte : UInt8 = 0xF3 //Should become "F3"

I have tried this method that won't compile when passing-in either a byte or a byte array.

Louis
  • 1,913
  • 2
  • 28
  • 41
  • Have you tried this answer: http://stackoverflow.com/a/39772685/4228969 – toddg Dec 15 '16 at 15:54
  • Possible duplicate of [How to convert UInt8 byte array to string in Swift](http://stackoverflow.com/questions/29643986/how-to-convert-uint8-byte-array-to-string-in-swift) – shallowThought Dec 15 '16 at 15:55
  • Possible duplicate of [Swift native functions to have numbers as hex strings](http://stackoverflow.com/questions/27189338/swift-native-functions-to-have-numbers-as-hex-strings). – Martin R Dec 15 '16 at 16:02
  • This might also help: [How to convert Data to hex string in swift](http://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift). – Martin R Dec 15 '16 at 16:04
  • @toddg this didn't work for me, but this response [link](http://stackoverflow.com/a/36286365/6114088) did the job thanks!! – Louis Dec 15 '16 at 16:25

1 Answers1

3

Two ways:

let s1 = String(byte, radix: 16, uppercase: true) // does not do 0-padding but works with
                                                  // all radices between 2 and 36

let s2 = String(format: "%02X", byte)             // very similar to C
Code Different
  • 90,614
  • 16
  • 144
  • 163