YourMJK
YourMJK

Reputation: 1629

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".

Upvotes: 53

Views: 45994

Answers (1)

Martin R
Martin R

Reputation: 539685

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)

Upvotes: 112

Related Questions