//
//  UIntExtension.swift
//  Tun
//
//  Created by 安礼成 on 2024/5/30.
//

import Foundation

extension UInt16 {
    init(bytes: (UInt8, UInt8)) {
        self = UInt16(bytes.0) << 8 + UInt16(bytes.1)
    }
    
    init(data: Data) {
        self = UInt16(data[0]) << 8 + UInt16(data[1])
    }
    
    func data() -> Data {
        var data = Data()
        
        data.append(contentsOf:  [UInt8(self >> 8), UInt8(self & 0x00FF)])
        
        return data
    }
}

extension UInt32 {
    init(bytes: (UInt8, UInt8, UInt8, UInt8)) {
        self = UInt32(bytes.0) << 24 + UInt32(bytes.1) << 16 + UInt32(bytes.2) << 8 + UInt32(bytes.3)
    }
    
    init(data: Data) {
        self = UInt32(data[0]) << 24 | UInt32(data[1]) << 16 | UInt32(data[2]) << 8 | UInt32(data[3])
    }
    
}
