UnicodeScalar. In Swift a Character can be constructed from a UnicodeScalar value. And we can build up a UnicodeScalar from an Int. This allows many conversions.
With utf8 and utf16, both properties on a String, we can access all Int values of the characters of a String. With these properties, we can apply number-based conversions to characters.
Int to character. This example converts an Int that represents an ASCII char to an actual Character. The number 97 indicates a lowercase "a."
Start We convert the value 97 to a UnicodeScalar instance with an init method. We then create a Character from that.
Result We find that the value 97 was converted into a Character that indicates a lowercase letter "a."
let value = 97
// Convert Int to a UnicodeScalar.
let u = UnicodeScalar(value)!
// Convert UnicodeScalar to a Character.
let char = Character(u)
// Write results.
print(char)a
Get Ints from Characters. Here we apply the utf16 and utf8 properties on a String. These return Ints that represent that characters in a String.
Note Utf16 returns UInt16 values. We can convert these to an Int or cast them with no errors.
Note 2 Utf8 returns UInt8 chars. It is important that the string not contain non-ASCII characters when we use this.
let value = "abc"
for u in value.utf16 {
// This value is a UInt16.
print(u)
}
print("") // Write newline.
for u in value.utf8 {
// This value is a UInt8.
print(u)
}97
98
99
97
98
99
ROT13. Some algorithms, like the ROT13 substitution cipher, require converting from characters to Ints. We can alter characters based on their numeric values.
A review. Every character has an underlying integer value. In Swift we can use this value, along with UnicodeScalar, to create a Character. This process is logical.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 23, 2023 (edit).