Sesame-Server/Sources/App/API/Extensions/UInt32+Coding.swift

23 lines
586 B
Swift
Raw Normal View History

2022-05-01 13:12:16 +02:00
import Foundation
2022-05-01 13:39:39 +02:00
import CoreFoundation
2022-05-01 13:12:16 +02:00
extension UInt32 {
/**
Create a value from a little-endian data representation (MSB first)
- Note: The data must contain exactly four bytes.
*/
init(data: Data) {
let value = data.convert(into: UInt32.zero)
self = CFSwapInt32LittleToHost(value)
}
/// The value encoded to a little-endian representation
var encoded: Data {
Data(from: CFSwapInt32HostToLittle(self))
}
2023-12-08 12:39:10 +01:00
/// The size of a `UInt32` when converted to data
static let byteSize = MemoryLayout<UInt32>.size
2022-05-01 13:12:16 +02:00
}