Last active
October 30, 2024 22:13
-
-
Save DevAndArtist/454971d4ccd2e4cd330830321eed5018 to your computer and use it in GitHub Desktop.
A cheat sheet for byte conversion of number types in Swift 3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func _convertToBytes<T>(_ value: T, withCapacity capacity: Int) -> [UInt8] { | |
var mutableValue = value | |
return withUnsafePointer(to: &mutableValue) { | |
return $0.withMemoryRebound(to: UInt8.self, capacity: capacity) { | |
return Array(UnsafeBufferPointer(start: $0, count: capacity)) | |
} | |
} | |
} | |
extension Integer { | |
var bytes: [UInt8] { | |
return _convertToBytes(self, withCapacity: MemoryLayout<Self>.size) | |
} | |
init?(_ bytes: [UInt8]) { | |
guard bytes.count == MemoryLayout<Self>.size else { return nil } | |
self = bytes.withUnsafeBytes { | |
return $0.load(as: Self.self) | |
} | |
} | |
} | |
extension FloatingPoint { | |
var bytes: [UInt8] { | |
return _convertToBytes(self, withCapacity: MemoryLayout<Self>.size) | |
} | |
init?(_ bytes: [UInt8]) { | |
guard bytes.count == MemoryLayout<Self>.size else { return nil } | |
self = bytes.withUnsafeBytes { | |
return $0.load(as: Self.self) | |
} | |
} | |
} | |
let u_int_1: UInt64 = 0x08_07_06_05_04_03_02_01 // 578437695752307201 | |
u_int_1.littleEndian.bytes // [1, 2, 3, 4, 5, 6, 7, 8] | |
u_int_1.bigEndian.bytes // [8, 7, 6, 5, 4, 3, 2, 1] | |
// | |
let u_int_2: UInt32 = 0x04_03_02_01 // 67305985 | |
u_int_2.littleEndian.bytes // [1, 2, 3, 4] | |
u_int_2.bigEndian.bytes // [4, 3, 2, 1] | |
// | |
let u_int_3: UInt16 = 0x02_01 // 513 | |
u_int_3.littleEndian.bytes // [1, 2] | |
u_int_3.bigEndian.bytes // [2, 1] | |
// | |
let int_1: Int64 = 0x08_07_06_05_04_03_02_01 // 578437695752307201 | |
int_1.littleEndian.bytes // [1, 2, 3, 4, 5, 6, 7, 8] | |
int_1.bigEndian.bytes // [8, 7, 6, 5, 4, 3, 2, 1] | |
// | |
let int_2: Int32 = 0x04_03_02_01 // 67305985 | |
int_2.littleEndian.bytes // [1, 2, 3, 4] | |
int_2.bigEndian.bytes // [4, 3, 2, 1] | |
// | |
let int_3: Int16 = 0x02_01 // 513 | |
int_3.littleEndian.bytes // [1, 2] | |
int_3.bigEndian.bytes // [2, 1] | |
let flp_1: Double = 4.0 // (on macOS) | |
flp_1.bytes // [0, 0, 0, 0, 0, 0, 16, 64] - in little endian | |
flp_1.bytes.reversed() as [UInt8] // [64, 16, 0, 0, 0, 0, 0, 0] | |
// | |
let value_1 = UInt64(0x40_10_00_00_00_00_00_00) // 4616189618054758400 | |
let flp_2 = unsafeBitCast(value_1, to: Double.self) // 4.0 | |
// | |
let value_2: UInt64 = UInt64(flp_1.bytes)! // 4616189618054758400 | |
let flp_3 = unsafeBitCast(value_2, to: Double.self) // 4.0 | |
// | |
let flp_4: Float64 = 123456.123456 // | |
flp_4.bytes // [168, 255, 172, 249, 1, 36, 254, 64] | |
Float64(flp_4.bytes) // 123456.123456 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment