Swift: How to Convert a String to Uint8 Array

Swift: How to convert a String to UInt8 array?

Lots of different ways, depending on how you want to handle non-ASCII characters.

But the simplest code would be to use the utf8 view:

let string = "hello"

let array: [UInt8] = Array(string.utf8)

Note, this will result in multi-byte characters being represented as multiple entries in the array, i.e.:

let string = "é"
print(Array(string.utf8))

prints out [195, 169]

There’s also .nulTerminatedUTF8, which does the same thing, but then adds a nul-character to the end if your plan is to pass this somewhere as a C string (though if you’re doing that, you can probably also use .withCString or just use the implicit conversion for bridged C functions.

How to convert Character to UInt8 in Swift

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

How to convert hexadecimal string to an array of UInt8 bytes in Swift?

You can convert your hexa String back to array of [UInt8] iterating every two hexa characters and initialize an UInt8 using its string radix initializer. The following implementation assumes the hexa string is well formed:


Edit/update: Xcode 11 • Swift 5.1

extension StringProtocol {
var hexaData: Data { .init(hexa) }
var hexaBytes: [UInt8] { .init(hexa) }
private var hexa: UnfoldSequence<UInt8, Index> {
sequence(state: startIndex) { startIndex in
guard startIndex < self.endIndex else { return nil }
let endIndex = self.index(startIndex, offsetBy: 2, limitedBy: self.endIndex) ?? self.endIndex
defer { startIndex = endIndex }
return UInt8(self[startIndex..<endIndex], radix: 16)
}
}
}


let string = "e0696349774606f1b5602ffa6c2d953f"
let data = string.hexaData // 16 bytes
let bytes = string.hexaBytes // [224, 105, 99, 73, 119, 70, 6, 241, 181, 96, 47, 250, 108, 45, 149, 63]

If you would like to handle malformed hexa strings as well you can make it a throwing method:

extension String {
enum DecodingError: Error {
case invalidHexaCharacter(Character), oddNumberOfCharacters
}
}


extension Collection {
func unfoldSubSequences(limitedTo maxLength: Int) -> UnfoldSequence<SubSequence,Index> {
sequence(state: startIndex) { lowerBound in
guard lowerBound < endIndex else { return nil }
let upperBound = index(lowerBound,
offsetBy: maxLength,
limitedBy: endIndex
) ?? endIndex
defer { lowerBound = upperBound }
return self[lowerBound..<upperBound]
}
}
}


extension StringProtocol {
func hexa<D>() throws -> D where D: DataProtocol & RangeReplaceableCollection {
try .init(self)
}
}


extension DataProtocol where Self: RangeReplaceableCollection {
init<S: StringProtocol>(_ hexa: S) throws {
guard hexa.count.isMultiple(of: 2) else {
throw String.DecodingError.oddNumberOfCharacters
}
self = .init()
reserveCapacity(hexa.utf8.count/2)
for pair in hexa.unfoldSubSequences(limitedTo: 2) {
guard let byte = UInt8(pair, radix: 16) else {
for character in pair where !character.isHexDigit {
throw String.DecodingError.invalidHexaCharacter(character)
}
continue
}
append(byte)
}
}
}

Usage:

let hexaString = "e0696349774606f1b5602ffa6c2d953f"
do {
let bytes: [UInt8] = try hexaString.hexa()
print(bytes)
let data: Data = try hexaString.hexa()
print(data)
} catch {
print(error)
}

This will print

[224, 105, 99, 73, 119, 70, 6, 241, 181, 96, 47, 250, 108, 45, 149, 63]

16 bytes

Swift convert decimal String to UInt8-Array

Edit: Updated for Swift 5

I wrote you a function to convert your number string. This is written in Swift 5 (originally Swift 1.2).

func decimalStringToUInt8Array(_ decimalString: String) -> [UInt8] {

// Convert input string into array of Int digits
let digits = Array(decimalString).compactMap { Int(String($0)) }

// Nothing to process? Return an empty array.
guard digits.count > 0 else { return [] }

let numdigits = digits.count

// Array to hold the result, in reverse order
var bytes = [UInt8]()

// Convert array of digits into array of Int values each
// representing 6 digits of the original number. Six digits
// was chosen to work on 32-bit and 64-bit systems.
// Compute length of first number. It will be less than 6 if
// there isn't a multiple of 6 digits in the number.
var ints = Array(repeating: 0, count: (numdigits + 5)/6)
var rem = numdigits % 6
if rem == 0 {
rem = 6
}
var index = 0
var accum = 0
for digit in digits {
accum = accum * 10 + digit
rem -= 1
if rem == 0 {
rem = 6
ints[index] = accum
index += 1
accum = 0
}
}

// Repeatedly divide value by 256, accumulating the remainders.
// Repeat until original number is zero
while ints.count > 0 {
var carry = 0
for (index, value) in ints.enumerated() {
var total = carry * 1000000 + value
carry = total % 256
total /= 256
ints[index] = total
}

bytes.append(UInt8(truncatingIfNeeded: carry))

// Remove leading Ints that have become zero.
while ints.count > 0 && ints[0] == 0 {
ints.remove(at: 0)
}
}

// Reverse the array and return it
return bytes.reversed()
}

print(decimalStringToUInt8Array("0")) // prints "[0]"
print(decimalStringToUInt8Array("255")) // prints "[255]"
print(decimalStringToUInt8Array("256")) // prints "[1,0]"
print(decimalStringToUInt8Array("1024")) // prints "[4,0]"
print(decimalStringToUInt8Array("16777216")) // prints "[1,0,0,0]"

Here's the reverse function. You'll notice it is very similar:

func uInt8ArrayToDecimalString(_ uint8array: [UInt8]) -> String {

// Nothing to process? Return an empty string.
guard uint8array.count > 0 else { return "" }

// For efficiency in calculation, combine 3 bytes into one Int.
let numvalues = uint8array.count
var ints = Array(repeating: 0, count: (numvalues + 2)/3)
var rem = numvalues % 3
if rem == 0 {
rem = 3
}
var index = 0
var accum = 0
for value in uint8array {
accum = accum * 256 + Int(value)
rem -= 1
if rem == 0 {
rem = 3
ints[index] = accum
index += 1
accum = 0
}
}

// Array to hold the result, in reverse order
var digits = [Int]()

// Repeatedly divide value by 10, accumulating the remainders.
// Repeat until original number is zero
while ints.count > 0 {
var carry = 0
for (index, value) in ints.enumerated() {
var total = carry * 256 * 256 * 256 + value
carry = total % 10
total /= 10
ints[index] = total
}

digits.append(carry)

// Remove leading Ints that have become zero.
while ints.count > 0 && ints[0] == 0 {
ints.remove(at: 0)
}
}

// Reverse the digits array, convert them to String, and join them
return digits.reversed().map(String.init).joined()
}

Doing a round trip test to make sure we get back to where we started:

let a = "1234567890987654321333555777999888666444222000111"
let b = decimalStringToUInt8Array(a)
let c = uInt8ArrayToDecimalString(b)
if a == c {
print("success")
} else {
print("failure")
}
success

Check that eight 255 bytes is the same as UInt64.max:

print(uInt8ArrayToDecimalString([255, 255, 255, 255, 255, 255, 255, 255]))
print(UInt64.max)
18446744073709551615
18446744073709551615


Related Topics



Leave a reply



Submit