Newer Older
36 lines | 1.008kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  MacAdress.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 04/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import Foundation
10

            
11
class MACAddress {
12
    var bytes: [UInt8] = [0,0,0,0,0,0]
13

            
14
    init(from data: Data) {
15
        data.copyBytes(to: &bytes, count: bytes.count)
16
    }
17

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

            
26
extension MACAddress : CustomStringConvertible {
27
    var description: String {
28
        var retval = String(format: "%02X", bytes[0] )
29
        retval += ":" + String(format: "%02X", bytes[1] )
30
        retval += ":" + String(format: "%02X", bytes[2] )
31
        retval += ":" + String(format: "%02X", bytes[3] )
32
        retval += ":" + String(format: "%02X", bytes[4] )
33
        retval += ":" + String(format: "%02X", bytes[5] )
34
        return retval;
35
    }
36
}