swift4.2蓝牙开发笔记

最近在用swift重写公司蓝牙项目的代码,所以做个笔记记录一下基础知识的使用

1.字节数组转化为Data

let byte: [UInt8] = [0x23, 0xFF]
let data = Data(bytes: byte, count: 2)//NSData(bytes: byte, length: 2)
print(data as NSData) // <23ff>

2.Int转化为Data

var num = 12
let data1 = Data(bytes: &num, count: MemoryLayout.size(ofValue: num))
print(data1 as NSData) //<0c000000 00000000>

let data2 = NSData(bytes: &num, length: 1)
print(data2) //<0c>

let data3 = NSData(bytes: &num, length: 8)
print(data3) //<0c000000 00000000>

3.String转化为Data

let str = "string"
let data = str.data(using: .utf8)
print(data! as NSData) //<73747269 6e67>

4.String->Int->Data

let str = "2019"
var num = Int(str)!
let data = NSData(bytes: &num, length: 2)
print(data) //

let aaa = Data(bytes: &num, count: 2)
print(aaa as NSData) //

当然,还是写成swift的extension方便使用

  • extension
extension Data {
    
    var uint8: UInt8 {
        get {
            var number: UInt8 = 0
            self.copyBytes(to:&number, count: MemoryLayout.size)
            return number
        }
    }
    
    var uint16: UInt16 {
        get {
            let i16array = self.withUnsafeBytes {
                UnsafeBufferPointer(start: $0, count: self.count/2).map(UInt16.init(littleEndian:))
            }
            return i16array[0]
        }
    }
    
    var uint32: UInt32 {
        get {
            let i32array = self.withUnsafeBytes {
                UnsafeBufferPointer(start: $0, count: self.count/2).map(UInt32.init(littleEndian:))
            }
            return i32array[0]
        }
    }
    
    var uuid: NSUUID? {
        get {
            var bytes = [UInt8](repeating: 0, count: self.count)
            self.copyBytes(to:&bytes, count: self.count * MemoryLayout.size)
            return NSUUID(uuidBytes: bytes)
        }
    }
    var stringASCII: String? {
        get {
            return NSString(data: self, encoding: String.Encoding.ascii.rawValue) as String?
        }
    }
    
    var stringUTF8: String? {
        get {
            return NSString(data: self, encoding: String.Encoding.utf8.rawValue) as String?
        }
    }

    struct HexEncodingOptions: OptionSet {
        let rawValue: Int
        static let upperCase = HexEncodingOptions(rawValue: 1 << 0)
    }
    
    func hexEncodedString(options: HexEncodingOptions = []) -> String {
        let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx"
        return map { String(format: format, $0) }.joined()
    }
    
}

extension Int {
    var data: Data {
        var int = self
        return Data(bytes: &int, count: MemoryLayout.size)
    }
}

extension UInt8 {
    var data: Data {
        var int = self
        return Data(bytes: &int, count: MemoryLayout.size)
    }
}

extension UInt16 {
    var data: Data {
        var int = self
        return Data(bytes: &int, count: MemoryLayout.size)
    }
}

extension UInt32 {
    var data: Data {
        var int = self
        return Data(bytes: &int, count: MemoryLayout.size)
    }
    
    var byteArrayLittleEndian: [UInt8] {
        return [
            UInt8((self & 0xFF000000) >> 24),
            UInt8((self & 0x00FF0000) >> 16),
            UInt8((self & 0x0000FF00) >> 8),
            UInt8(self & 0x000000FF)
        ]
    }
}

你可能感兴趣的:(swift4.2蓝牙开发笔记)