JBaczuk
JBaczuk

Reputation: 14589

How to convert Character to UInt8 in Swift

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?

Upvotes: 7

Views: 14011

Answers (5)

Hack Saw
Hack Saw

Reputation: 2781

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.

Upvotes: 1

Vyacheslav
Vyacheslav

Reputation: 27211

To improve @dfri answer https://stackoverflow.com/a/36025104/1979882

for Swift 3 you have to use this:

extension Sequence where Iterator.Element == Character {

    /* extension accessible as function */
    func asByteArray() -> [UInt8] {
        return String(self).utf8.map{UInt8($0)}
    }

    /* or, as @LeoDabus pointed out below (thanks!),
     use a computed property for this simple case  */
    var byteArray : [UInt8] {
        return String(self).utf8.map{UInt8($0)}
    }
}

Upvotes: 0

dfrib
dfrib

Reputation: 73166

You will need to go via the String representation of the Character:s to convert to UInt8. You needn't, however, explicitly initialize an array in your [Character] -> [UInt8] conversion; since String.UTF8View (from String.utf8) is a CollectionType, you can apply a map operation on the String.UTF8View itself; with an UInt8 initialization. I.e.,

let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"]
let asUInt8Array = String(charArray).utf8.map{ UInt8($0) }

print(asUInt8Array)
/* [83, 116, 114, 105, 110, 103] */

print(asUInt8Array.dynamicType)
/* Array<UInt8> */

With regard to your comment below ("frustrated over the abstraction of Swift, as compared to the simple ways of Objective-C"): if you believe the above to be messy, you could include it in an extension to SequenceType constrained to Character elements, allowing easier use in practice. E.g.:

extension SequenceType where Generator.Element == Character {

    /* extension accessible as function */
    func asByteArray() -> [UInt8] {
        return String(self).utf8.map{UInt8($0)}
    }

    /* or, as @LeoDabus pointed out below (thanks!),
       use a computed property for this simple case  */
    var byteArray : [UInt8] {
        return String(self).utf8.map{UInt8($0)}
    }
}

Usage example:

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

/* use extension function */
let asUInt8Array = charArray.asByteArray()

/* or computed property */
let asUInt8Array = charArray.byteArray

Upvotes: 17

JBaczuk
JBaczuk

Reputation: 14589

This is how I ended up doing it:

var char: Character = "a"
var byte: UInt8 = Array(String(char).utf8)[0]

There must be a better way...

Upvotes: 2

Michael
Michael

Reputation: 9044

I'm not sure how to convert a Character to UTF-8, but a String has a utf8 property, so you could use the following:

let charArray: [Character] = [ "S", "t", "r", "i", "n", "g"]
let string = Array(String(charArray).utf8)
print(string)

Upvotes: 4

Related Questions