jon jon - 4 months ago 44
Swift Question

how do i convert a hex string to its unicode ascii equivalent in swift?

i've been trying to convert a hexadecimal string im getting via serial to something i can use as an ascii string using swift.

for example, if this is the string: "<01736c65 65702061 6e642062 61747465 72206c65>"

i want to convert it to a string that says "sleep and batter le"

note: that we would have to remove the carrots at the front and end of the string.

ive been going at this for two days and there isnt an elegant or simple solution. any ideas would be really appreciated.

I got this far,

func hex2ascii (example: String) -> String {

let chars = Array(example)
let numbers = map (stride(from: 0, to: chars.count, by: 2)) {
strtoul(String(chars[$0 ..< $0+2]), nil, 16)
}

var final = ""
var i = 0

while i < numbers.count {
final.append(Character(UnicodeScalar(Int(numbers[i]))))
i++
}
return final
}


for example, an input "<01736c65 65702061 6e642062 61747465 72206c65>" returns this`"6Æep aæBatte"Æ"'

obviously not right, how to do fix this?

Answer

It seems you are not stripping off the angle braces and spaces between your hex digits.

The output you get "6Æep aæBatte"Æ"corresponds to this unicode entities (with spaces added):

%22 %36 %C6 %65 %70 %20 %61 %E6 %62 %61 %74 %74 %65 %22 %C6 %22

I don't know where the double quotes come from, but comparing it with the original hex string you can see that the spaces make you loose a digit, displacing the numbers.

<01736c65 65702061 6e642062 61747465 72206c65>

Note the chars %36, %c6 and %E6 produced by the character shift.

I used this page to encode and decode.

Comments