Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get string from ASCII code in Swift?

How can I get a character from a ASCII code in Apple's new Swift?

For example 65 would returns "A".

like image 383
YourMJK Avatar asked Jun 22 '14 18:06

YourMJK


People also ask

How to get ASCII value of character in Swift?

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.

How do you increment a character in Swift?

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.

Is there a character type in Swift?

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.


1 Answers

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)

like image 183
Martin R Avatar answered Sep 18 '22 21:09

Martin R