I wrote a function in swift hex2ascii
, which takes a hex string and converts to an ascii string. It looks like its working when i output the string to a text field using, charOutput.text = "\(asciidata)"
(charOutput is hte name of the text field), however when i try to append this string to an array, using arrayName.append, the string looks corrupted -- meaning its pure garbage. please help!
the hex2ascii function is:
func hex2ascii (example: String) -> String {
var example = example.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>")).stringByReplacingOccurrencesOfString(" ", withString: "")
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 = 2
while i < (numbers.count - 2) {
final.append(Character(UnicodeScalar(Int(numbers[i]))))
i++
}
return final
}
here is the function where i am am calling this hex2ascii function, and trying to append it to array (which is than drawn in the UI)
func output(description: String, data: AnyObject){
println("\(data)")
var asciidata = hex2ascii("\(data)")
println("\(description): \(asciidata)")
charOutput.text = "\(asciidata)"
results.append ("\(asciidata)")
self.tableView.reloadData()
println("array has \(results.count) rows")
println("\(asciidata)")
dump(results)
}