How can I get a character from a ASCII code in Apple's new Swift?
For example 65
would returns "A"
.
Overview. In Swift, every numerical ASCII value has its corresponding and equivalent character value. To convert an ASCII value to its equivalent character, we use the UnicodeScalar() function.
Swift lets us to use the += operator to add another String or an array of characters to the original String , but not a single character.
Swift CharacterCharacter is a data type that represents a single-character string ( "a" , "@" , "5" , etc). Here, the letter variable can only store single-character data.
As a character:
let c = Character(UnicodeScalar(65))
Or as a string:
let s = String(UnicodeScalar(UInt8(65)))
Or another way as a string:
let s = "\u{41}"
(Note that the \u
escape sequence is in hexadecimal, not decimal)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With