3

I have a data source in String example

HexString = "72AE"

and i would like to convert it into byte and store into byte array

bytearray = [72, AE]  //UInt8

i know i can do this by

let hexaString = "72AE"
let resultArray = hexaString.characters.map{Int(strtoul(( String($0)), nil, 16))}

print(resultArray)  // "[7, 2, 10, 14]"

but it is not returning the value i want. I have also tried to chop it into hexaString1 = "72" hexaString2 = "AE" but still i can't manage to get the correct value.

mah
  • 39,056
  • 9
  • 76
  • 93
wes. i
  • 627
  • 9
  • 26

1 Answers1

1

Hope this will help you

let hexaString = "72AE"
var byteArray = [UInt8]()
byteArray += hexaString.utf8  // Convert into byte array

// Retain the orginal string from byte array
let stringFromByteArray = NSString(bytes: byteArray, length: byteArray.count, encoding: NSUTF8StringEncoding)
Muzahid
  • 5,072
  • 2
  • 24
  • 42