How to convert Character to UInt8 in Swift

Viewed 11701

I've got an array of Characters in swift:

static let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"]

And I'd like to do some bitwise operations on each Character as a byte (UInt8). How do I convert charArray[0] to UInt8, for example?

5 Answers

Swift 5:

let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"]

var oneAscii: UInt8 = charArray[0].asciiValue!

Of course, if your input isn't a literal from the ASCII set, you want to be making sure the result isn't nil.

Related