28 lines
702 B
Swift
28 lines
702 B
Swift
|
import Foundation
|
||
|
|
||
|
extension Int {
|
||
|
|
||
|
var twoByteData: Data {
|
||
|
let value = UInt16(clamping: self)
|
||
|
return Data([value.low, value.high])
|
||
|
}
|
||
|
|
||
|
var fourByteData: Data {
|
||
|
let value = UInt32(clamping: self)
|
||
|
return Data([
|
||
|
UInt8(value & 0xFF),
|
||
|
UInt8((value >> 8) & 0xFF),
|
||
|
UInt8((value >> 16) & 0xFF),
|
||
|
UInt8((value >> 24) & 0xFF)
|
||
|
])
|
||
|
}
|
||
|
|
||
|
init(uint32 byte0: UInt8, _ byte1: UInt8, _ byte2: UInt8, _ byte3: UInt8) {
|
||
|
self = (Int(byte0) << 24) | (Int(byte1) << 16) | (Int(byte2) << 8) | Int(byte3)
|
||
|
}
|
||
|
|
||
|
init(high: UInt8, low: UInt8) {
|
||
|
self = Int(high) << 8 + Int(low)
|
||
|
}
|
||
|
}
|