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.

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I assume he wanted 0x72 and 0xAE returned, That just gets 0x37 (7), 0x32 (2), 0x41 (A), 0x45 (E)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.