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?
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]
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])
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With