//
//  DataFromHex.swift
//  AirbridgeReactNative
//
//  Created by WOF on 7/8/24.
//

import Foundation

extension Data {
    init?(hex: String) {
        guard hex.count % 2 == 0 else { return nil }
        
        let characters = hex.map { $0 }
        let bytes = stride(from: 0, to: characters.count, by: 2)
            .map { String(characters[$0]) + String(characters[$0 + 1]) }
            .compactMap { UInt8($0, radix: 16) }
        
        self.init(bytes)
    }
}
