Reputation: 75
I am having trouble converting a hex string that is inputted by the user (i.e. "1F436") to a unicode character I can work with.
From reading the Swift documentation, I learned how to print unicode characters using 'print(\u{1F436})' and how to to figure out the decimal value of unicode characters in a string by using for-in loops.
But how do I go about creating a unicode character variable from a string that contains its hexadecimal number?
Upvotes: 4
Views: 881
Reputation: 14973
Very simple, just like this:
let inputText = "1F436"
let character = Int(inputText, radix: 16).map{ Character(UnicodeScalar($0)) }
character
is an optional Character
depending on whether the number was successfully parsed
The notation "\u{1F436}" is only possible to use from the programmer, as it gets translated to the actual character at compile time
Upvotes: 5