美文网首页
swift4.2蓝牙开发笔记

swift4.2蓝牙开发笔记

作者: Mr_Coii | 来源:发表于2019-02-27 13:18 被阅读0次

最近在用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) //<e307>

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

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

  • extension
extension Data {
    
    var uint8: UInt8 {
        get {
            var number: UInt8 = 0
            self.copyBytes(to:&number, count: MemoryLayout<UInt8>.size)
            return number
        }
    }
    
    var uint16: UInt16 {
        get {
            let i16array = self.withUnsafeBytes {
                UnsafeBufferPointer<UInt16>(start: $0, count: self.count/2).map(UInt16.init(littleEndian:))
            }
            return i16array[0]
        }
    }
    
    var uint32: UInt32 {
        get {
            let i32array = self.withUnsafeBytes {
                UnsafeBufferPointer<UInt32>(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<UInt32>.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<Int>.size)
    }
}

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

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

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

相关文章

  • 【Objective-c】_蓝牙开发

    ios蓝牙开发学习笔记(一)蓝牙概述 ios蓝牙开发学习笔记(二)central角色的实现 ios蓝牙开发学习笔记...

  • swift4.2蓝牙开发笔记

    最近在用swift重写公司蓝牙项目的代码,所以做个笔记记录一下基础知识的使用 1.字节数组转化为Data 2.In...

  • iOS蓝牙开发学习笔记 - 系列

    详见我的csdn: iOS蓝牙开发学习笔记(一)蓝牙概述 iOS蓝牙开发学习笔记(二)central角色的实现 i...

  • ios蓝牙参考

    参考文章:iOS中的蓝牙开发iOS-BLE蓝牙开发demo 官网 转载 CenteralManager学习笔记

  • 2018-02-08

    IOS蓝牙开发SDK通信笔记连接和读写 当下蓝牙开发可谓是越来越火,不论是...

  • 美剧Mac客户端

    swift4.2开发 github地址:https://github.com/imeiju/iMeiJu_Mac

  • iOS蓝牙开发(四):BabyBluetooth蓝牙库介绍

    iOS蓝牙开发(四):BabyBluetooth蓝牙库介绍 iOS蓝牙开发(四):BabyBluetooth蓝牙库介绍

  • iOS BLE蓝牙单例封装

    公司是一家做蓝牙只能硬件的公司,所以整理一些蓝牙开发的笔记。 一个好用的第三方蓝牙库:BabyBluetooth ...

  • iOS蓝牙开发相关知识点和注意事项

    总结一下蓝牙开发相关的知识点和注意事项,做个笔记,也希望你们能少踩坑 (公司部分蓝牙项目为混编项目,蓝牙相关处理均...

  • iOS 项目切换内外网

    语言:swift4.2 IDE: Xcode 10 目的:iOS项目切换开发网络环境 设置Xcode PROJEC...

网友评论

      本文标题:swift4.2蓝牙开发笔记

      本文链接:https://www.haomeiwen.com/subject/klfybxtx.html