ios/BitwardenWatchShared/MessagePack/FixedWidthInteger+Bytes.swift
2024-01-16 10:01:12 -07:00

20 lines
585 B
Swift

extension FixedWidthInteger {
init(bytes: [UInt8]) {
self = bytes.withUnsafeBufferPointer {
$0.baseAddress!.withMemoryRebound(to: Self.self, capacity: 1) {
$0.pointee
}
}.bigEndian
}
var bytes: [UInt8] {
let capacity = MemoryLayout<Self>.size
var mutableValue = bigEndian
return withUnsafePointer(to: &mutableValue) {
$0.withMemoryRebound(to: UInt8.self, capacity: capacity) {
Array(UnsafeBufferPointer(start: $0, count: capacity))
}
}
}
}