4

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?

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.