10

I want to add UInt32 to byte buffer for which I use [UInt8]. In java, there is convenient ByteBuffer class that has methods like putInt() for cases exactly like this. How could this be done in swift?

I guess I could solve this as following:

let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15
var byteArray = [UInt8](count: 4, repeatedValue: 0)

for i in 0...3 {
    byteArray[i] = UInt8(0x0000FF & example >> UInt32((3 - i) * 8))
}

This is quite verbose though, any simpler way?

Kelo
  • 453
  • 1
  • 3
  • 17

7 Answers7

17

Your loop can more compactly be written as

let byteArray = 24.stride(through: 0, by: -8).map {
    UInt8(truncatingBitPattern: example >> UInt32($0))
}

Alternatively, create an UnsafeBufferPointer and convert that to an array:

let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15

var bigEndian = example.bigEndian
let bytePtr = withUnsafePointer(&bigEndian) {
    UnsafeBufferPointer<UInt8>(start: UnsafePointer($0), count: sizeofValue(bigEndian))
}
let byteArray = Array(bytePtr)

print(byteArray) // [72, 66, 1, 15]

Update for Swift 3 (Xcode 8 beta 6):

var bigEndian = example.bigEndian
let count = MemoryLayout<UInt32>.size
let bytePtr = withUnsafePointer(to: &bigEndian) {
    $0.withMemoryRebound(to: UInt8.self, capacity: count) {
        UnsafeBufferPointer(start: $0, count: count)
    }
}
let byteArray = Array(bytePtr)
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks! At first glance this loop seems harder to read, but it sure is more compact. – Kelo May 01 '15 at 23:24
5

I had a similar need and was trying to learn a bit about manual memory management. Am I missing something, or has this gotten easier with newer versions of Swift? (I'm using Swift 5)

Creating an array of bytes ([UInt8]) from a UInt32

let example: UInt32 = 1

let byteArray = withUnsafeBytes(of: example.bigEndian) {
    Array($0)
}

print(byteArray) // [0, 0, 0, 1]

or if you want to append to an existing array, that can be done in the closure instead:

var existingArray: [UInt8] = [1, 2, 3]
let example: UInt32 = 1

withUnsafeBytes(of: example.bigEndian) {
    existingArray.append(contentsOf: $0)
}

print(existingArray)  // [1, 2, 3, 0, 0, 0, 1]
user1325158
  • 194
  • 3
  • 9
4

Improved @Martin R's answered. Worked on UInt16, UInt32, and UInt64:

protocol UIntToBytesConvertable {
    var toBytes: [Byte] { get }
}

extension UIntToBytesConvertable {
    func toByteArr<T: Integer>(endian: T, count: Int) -> [Byte] {
        var _endian = endian
        let bytePtr = withUnsafePointer(to: &_endian) {
            $0.withMemoryRebound(to: Byte.self, capacity: count) {
                UnsafeBufferPointer(start: $0, count: count)
            }
        }
        return [Byte](bytePtr)
    }
}

extension UInt16: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt16>.size)
    }
}

extension UInt32: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt32>.size)
    }
}

extension UInt64: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt64>.size)
    }
}
Benson
  • 129
  • 1
  • 7
3

You can cast from one UnsafeMutablePointer type to another:

var arr = UnsafeMutablePointer<UInt32>.alloc(1)
arr.memory = example
var arr2 = UnsafeMutablePointer<UInt8>(arr)
matt
  • 515,959
  • 87
  • 875
  • 1,141
1

Improved @Martin R's answered.

func toByteArrary<T>(value: T)  -> [UInt8] where T: UnsignedInteger, T: FixedWidthInteger{
  var bigEndian = value.bigEndian
  let count = MemoryLayout<T>.size
  let bytePtr = withUnsafePointer(to: &bigEndian) {
    $0.withMemoryRebound(to: UInt8.self, capacity: count) {
        UnsafeBufferPointer(start: $0, count: count)
    }
  }

  return Array(bytePtr)
}
Alfy
  • 889
  • 1
  • 7
  • 17
1

Another option is to extend the FixedWidthInteger protocol directly, so any UnsignedInteger automatically gets the function for free. This is how it looks building on @Benson's answer:

extension FixedWidthInteger where Self: UnsignedInteger {

    var bytes: [UInt8] {
        var _endian = littleEndian
        let bytePtr = withUnsafePointer(to: &_endian) {
            $0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<Self>.size) {
                UnsafeBufferPointer(start: $0, count: MemoryLayout<Self>.size)
            }
        }
        return [UInt8](bytePtr)
    }

}

We can verify this with a unit test:

func test_bytes() {
    XCTAssertEqual(UInt8.min.bytes, [0])
    XCTAssertEqual(UInt8.max.bytes, [255])

    XCTAssertEqual(UInt16.min.bytes, [0, 0])
    XCTAssertEqual(UInt16.max.bytes, [255, 255])

    XCTAssertEqual(UInt32.min.bytes, [0, 0, 0, 0])
    XCTAssertEqual(UInt32.max.bytes, [255, 255, 255, 255])

    XCTAssertEqual(UInt64.min.bytes, [0, 0, 0, 0, 0, 0, 0, 0])
    XCTAssertEqual(UInt64.max.bytes, [255, 255, 255, 255, 255, 255, 255, 255])
}
Kane Cheshire
  • 1,654
  • 17
  • 20
0

I have a similar problem with Xcode 8 Beta 6: write this line

var value = String(cString: sqlite3_column_text(stmt, index))

   to


let txt = UnsafePointer<Int8>(sqlite3_column_text(stmt, index))

and solve problems

pradip kikani
  • 627
  • 6
  • 14