Fix encoding error

This commit is contained in:
Christoph Hagen
2023-12-20 09:25:53 +01:00
parent b749a80f5d
commit ad94588b3c
6 changed files with 30 additions and 13 deletions

View File

@ -2,12 +2,6 @@ import Foundation
extension Data {
func convert<T>(into value: T) -> T {
withUnsafeBytes {
$0.baseAddress!.load(as: T.self)
}
}
init<T>(from value: T) {
var target = value
self = Swift.withUnsafeBytes(of: &target) {

View File

@ -6,8 +6,8 @@ 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)
init(bytes: [UInt8]) {
let value = bytes.convert(to: UInt32.self)
self = CFSwapInt32LittleToHost(value)
}

View File

@ -0,0 +1,12 @@
import Foundation
extension Array where Element == UInt8 {
func convert<T>(to _: T.Type) -> T {
withUnsafeBufferPointer {
$0.baseAddress!.withMemoryRebound(to: T.self, capacity: 1) {
$0.pointee
}
}
}
}