swiftlib_sdlan/Sources/sdlan/DataExtension.swift
2025-07-14 15:33:40 +08:00

40 lines
837 B
Swift

//
// DataExtension.swift
// Tun
//
// Created by on 2024/2/29.
//
import Foundation
extension Data {
init(uint32: UInt32) {
var bytes: [UInt8] = [UInt8](repeating: 0, count: 4)
bytes[0] = (UInt8)(uint32 >> 24 & 0xFF)
bytes[1] = (UInt8)(uint32 >> 16 & 0xFF)
bytes[2] = (UInt8)(uint32 >> 8 & 0xFF)
bytes[3] = (UInt8)(uint32 & 0xFF)
self.init(bytes)
}
init(uint16: UInt16) {
var bytes: [UInt8] = [UInt8](repeating: 0, count: 2)
bytes[0] = (UInt8)(uint16 >> 8 & 0xFF)
bytes[1] = (UInt8)(uint16 & 0xFF)
self.init(bytes)
}
init(components: Data...) {
var data = Data()
for component in components {
data.append(component)
}
self = data
}
}