//
//  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
    }
    
}

