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?