I have converted the string "UK" into hex using code below. How can I convert it to hex?
let str = auxText
let bytes = str.utf8
var buffer = [UInt8](bytes)
buffer[0] = buffer[0] + UInt8(1)
print("ascii value is",buffer)
I have converted the string "UK" into hex using code below. How can I convert it to hex?
let str = auxText
let bytes = str.utf8
var buffer = [UInt8](bytes)
buffer[0] = buffer[0] + UInt8(1)
print("ascii value is",buffer)
In Swift 4, you can convert your ascii encoded string
to data with
string.data(using: .ascii)
To convert the data to hex or decimal you can use the following extensions:
extension Data {
var hexString: String {
return map { String(format: "%02hhx", $0) }.joined()
}
var decimalString: String {
return map { String(format: "%d", $0) }.joined()
}
}
Your test string "UK"
let string = "UK"
Evaluates with
let hexString = string.data(using: .ascii)!.hexString
To
554b
And with
let decimalString = string.data(using: .ascii)!.decimalString
To
8575
Both results are correct, you can look them up in any ASCII table.