简体   繁体   中英

Swift Hex to byte (iOS)

I have commands in hex format (for example 0x01, 0x02, etc)

How to enter them in xcode (what type of variables)? And how to convert them into bytes to calculate the checksum (CRC8)?

You can convert your hexa string back to array of UInt8 using this extension :

extension StringProtocol {
    var hexa2Bytes: [UInt8] {
        let hexa = Array(self)
        return stride(from: 0, to: count, by: 2).compactMap { UInt8(String(hexa[$0..<$0.advanced(by: 2)]), radix: 16) }
    }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM