Blame view

HDFwear/Tools/crc8.swift 536 Bytes
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  //
  //  crc8.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/8.
  //
  
  import UIKit
  
  func crc8(bytes: [UInt8]) -> UInt8 {
      if bytes.count == 0 {
          return 0x00
      }
      let len = bytes.count
  //    let bytes = [UInt8](data)
      var temp: UInt8 = 0xFF
  //    let len = data!.count
      for i in 0..<len {
          temp ^= bytes[i] & 0xFF
          for _ in 0..<8 {
              if temp & 0x1 != 0 {
                  temp = temp >> 1^0xB8
              } else {
                  temp >>= 1
              }
          }
      }
      return temp
  }