How to get string from ASCII code in Swift?
39,541
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)
Related videos on Youtube
Author by
YourMJK
Updated on July 09, 2022Comments
-
YourMJK almost 2 years
How can I get a character from a ASCII code in Apple's new Swift?
For example
65
would returns"A"
.-
Klaas over 9 yearsIf you know the code at compile time you just can use:
println("The Character A: \u{41}")
to print arbitrary unicode scalars. See developer.apple.com/library/ios/documentation/Swift/Conceptual/… and en.wikipedia.org/wiki/Basic_Latin_(Unicode_block)
-
-
YourMJK almost 10 yearsExactly what I needed. Thanks!
-
Martin R almost 10 years@M.J.K: See stackoverflow.com/questions/24102044/….
-
Martin R almost 7 years@nikoss: Updated so that it works with Swift 2 and 3. Thanks for letting me know!