Last active
December 25, 2019 05:05
-
-
Save monsoir/1bdadf3e482b65a92e5a08f17c0e56eb to your computer and use it in GitHub Desktop.
Data Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Data { | |
/// 将二进制数据转换为 Int8 类型数值 | |
var uint8: UInt8 { | |
var result: UInt8 = 0 | |
self.copyBytes(to: &result, count: MemoryLayout<UInt8>.size) | |
return result | |
} | |
/// 将二进制数据转换为 Bool 类型数值 | |
var bool: Bool { | |
guard let theData = first else { return false } | |
return theData == 0x01 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Data { | |
/// 将二进制数据转换为 16 进制字符串显示 | |
var hexString: String { | |
return self.reduce("", { $0 + String(format: "%02x", $1) }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment