1 contributor
36 lines | 1.008kb
//
//  MacAdress.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 04/03/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import Foundation

class MACAddress {
    var bytes: [UInt8] = [0,0,0,0,0,0]
    
    init(from data: Data) {
        data.copyBytes(to: &bytes, count: bytes.count)
    }

    init(from str: String) {
        let macAddressParts = str.split(separator: ":")
        for index in stride(from: 0, to: 5, by: 1){
            bytes[index] = UInt8(macAddressParts[index], radix: 16) ?? 0xff;
        }
    }
}

extension MACAddress : CustomStringConvertible {
    var description: String {
        var retval = String(format: "%02X", bytes[0] )
        retval += ":" + String(format: "%02X", bytes[1] )
        retval += ":" + String(format: "%02X", bytes[2] )
        retval += ":" + String(format: "%02X", bytes[3] )
        retval += ":" + String(format: "%02X", bytes[4] )
        retval += ":" + String(format: "%02X", bytes[5] )
        return retval;
    }
}