AutoTest.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. //
  2. // AutoTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/25.
  6. //
  7. import Foundation
  8. import AppKit
  9. var cacheObjects = NSMutableDictionary()
  10. class AutoTest : NSObject, AutoTestProtocal {
  11. var reportString : NSMutableAttributedString? = nil
  12. public var _status : AutoTestStatus = .Normal
  13. var _fileType : String = "RTF"
  14. var _type : String = "Others"
  15. var _extention : String = "rtf"
  16. var _degree : Double = 0.0
  17. var _fileDegree : [Double] = []
  18. class func autoTestFor(_ fileType:NSString ,type:NSString) -> AutoTest? {
  19. let key = String(fileType) + "." + String(type)
  20. if cacheObjects.value(forKey: key) != nil {
  21. let object = cacheObjects.value(forKey: key)
  22. return object as? AutoTest
  23. }
  24. // if let cacheObject as AutoTest {
  25. // return cacheObject
  26. // }
  27. let fileTypes = testTypeInfo[fileType] as! NSArray
  28. let clsname = "KdanAuto"//Bundle.main.infoDictionary! ["CFBundleExecutable"]
  29. for item in fileTypes {
  30. let cItem = item as! NSDictionary
  31. let cType = cItem["Type"] as! NSString
  32. let cExtention = cItem["Extention"] as! NSString
  33. if (cType.isEqual(to: type)) {
  34. let className = String((clsname )+"."+(cItem["Class"] as! String)) as! String
  35. let cl = NSClassFromString(className) as! AutoTest.Type
  36. let object = cl.shared()
  37. object?._fileType = fileType as! String
  38. object?._type = String(cType)
  39. object?._extention = String(cExtention)
  40. cacheObjects.setValue(object, forKey: key)
  41. return object
  42. }
  43. }
  44. let object = AutoTest.shared()
  45. object?._fileType = fileType as String
  46. object?._type = String(cType)
  47. object?._extention = ""
  48. cacheObjects.setValue(object, forKey: key)
  49. return object
  50. }
  51. class func shared() -> AutoTest? {
  52. return AutoTest()
  53. }
  54. func fileType() -> String {
  55. return _fileType
  56. }
  57. func type() -> String {
  58. return _type
  59. }
  60. func name() -> String {
  61. return "未指定类型对照测试"
  62. }
  63. func keys() -> NSArray {
  64. return ["快照"]
  65. }
  66. func selectedKeys() -> NSArray {
  67. let userDefaults = UserDefaults.standard
  68. let key = self.fileType() + "." + self.type() + ".selectedKeys"
  69. if userDefaults.value(forKey: key) != nil {
  70. return userDefaults.value(forKey: key) as! NSArray
  71. }
  72. self.setSelectedKeys(self.keys())
  73. return self.keys()
  74. }
  75. func setSelectedKeys(_ keys: NSArray) {
  76. let userDefaults = UserDefaults.standard
  77. let key = self.fileType() + "." + self.type() + ".selectedKeys"
  78. userDefaults.setValue(keys, forKey: key)
  79. userDefaults.synchronize()
  80. }
  81. func status() -> AutoTestStatus {
  82. return _status
  83. }
  84. func setStatus(_ status:AutoTestStatus) {
  85. _status = status
  86. }
  87. // Auto Test
  88. func autoTest() {
  89. clearCacheFiles()
  90. if hasOriginFile() {
  91. let needCompare = self.selectedKeys().contains("快照")
  92. if !needCompare {
  93. _status = .Finished
  94. return
  95. }
  96. _status = .Process
  97. reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】快照比对开始!\n",
  98. attributes:[.foregroundColor : NSColor.blue])
  99. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  100. let checkDirectory = self.checkFileDirectory()
  101. let originDirectory = self.originFileDirectory()
  102. let resultDirectory = self.resultFileDirectory()
  103. var degree = Double(0);
  104. var count = Int(0)
  105. for fileName in files {
  106. let fName = NSString(string: fileName).deletingPathExtension
  107. let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  108. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  109. let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  110. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】开始转换文件 \"\(fName)\"\n",
  111. attributes:[.foregroundColor : NSColor.blue]))
  112. // ...
  113. // 执行转换过程
  114. let convertSemaphore = DispatchSemaphore(value: 0)
  115. var success = false
  116. let convertQueue = DispatchQueue.global()
  117. convertQueue.async {
  118. success = FileConverter.shared().converter(originPath, inDesPath: resultPath)
  119. convertSemaphore.signal()
  120. }
  121. convertSemaphore.wait()
  122. var isDirectory = ObjCBool(false)
  123. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) && success {
  124. // compare screenshoot between result file with check file
  125. if needCompare {
  126. let items = NSMutableArray()
  127. if (isDirectory.boolValue) {
  128. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  129. for item in NSArray(array: searchItems) {
  130. let ext = NSString(string: item as! String).pathExtension.lowercased()
  131. if NSArray(array: [_extention]).contains(ext) {
  132. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  133. items.add(fileName)
  134. }
  135. }
  136. }else {
  137. items.add(fName)
  138. }
  139. var subDegree = Double(0);
  140. var subCount = Int(0)
  141. for item in items {
  142. let subFileName = item as! String
  143. let subResultPath = NSString(string: resultDirectory).appendingPathComponent(subFileName+"."+_extention)
  144. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subFileName+".\(_extention)")\"快照生成中\n",
  145. attributes:[.foregroundColor : NSColor.black]))
  146. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  147. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  148. let processThumbSemaphore = DispatchSemaphore(value: 0)
  149. var processSuccess = false
  150. let thumbnailQueue = DispatchQueue.global()
  151. thumbnailQueue.async {
  152. processSuccess = ProcessThumbnal.process(subResultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048))
  153. if ( processSuccess &&
  154. FileManager.default.fileExists(atPath: rComparePath)) {
  155. let degree = self.compareJPEG(rComparePath, checkPath: cComparePath)
  156. if degree == -1 {
  157. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比失败,生成快照失败或无比对文件\n",
  158. attributes:[.foregroundColor : NSColor.red]))
  159. }else {
  160. var color = NSColor.black
  161. if fabs(degree-100.0) >= 0.01 {
  162. color = NSColor.red
  163. }
  164. subDegree += degree
  165. subCount += 1
  166. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比完成,图像相似度 \(degree)%\n",
  167. attributes:[.foregroundColor : color]))
  168. }
  169. }else {
  170. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照生成失败\n",
  171. attributes:[.foregroundColor : NSColor.red]))
  172. }
  173. processThumbSemaphore.signal()
  174. }
  175. processThumbSemaphore.wait()
  176. }
  177. if subCount != 0 {
  178. subDegree = subDegree/Double(subCount)
  179. }else {
  180. subDegree = 0.0
  181. }
  182. _fileDegree.append(subDegree)
  183. degree += subDegree;
  184. count += 1
  185. }
  186. }else {
  187. reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n",
  188. attributes:[.foregroundColor : NSColor.red]))
  189. }
  190. }
  191. if count != 0 {
  192. _degree = degree/Double(count)
  193. }else {
  194. _degree = 0.0
  195. }
  196. _status = .Finished
  197. }else {
  198. _status = .Normal
  199. }
  200. }
  201. func testReport() -> NSAttributedString? {
  202. return reportString
  203. }
  204. func degree() -> Double {
  205. return _degree
  206. }
  207. // func testReportOfFile(_ fileName:String) -> NSAttributedString? {
  208. //
  209. // }
  210. func degreeOfFile(_ fileName:String) -> Double {
  211. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  212. let index = files.firstIndex(of: fileName)
  213. if index == NSNotFound || index! >= _fileDegree.count {
  214. return 0
  215. }else {
  216. return _fileDegree[index!]
  217. }
  218. }
  219. // Image compare
  220. func compareJPEG(_ resultPath:String, checkPath:String) -> Double {
  221. if !FileManager.default.fileExists(atPath: resultPath) || !FileManager.default.fileExists(atPath: checkPath) {
  222. return -1
  223. }
  224. let rImage = NSImage.init(contentsOfFile: resultPath) ?? nil
  225. let cImage = NSImage.init(contentsOfFile: checkPath) ?? nil
  226. if nil == rImage || nil == cImage {
  227. return -1
  228. }
  229. let resultImage = rImage as! NSImage
  230. let checkImage = cImage as! NSImage
  231. let resultImageRep = NSBitmapImageRep.init(cgImage: resultImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  232. let checkImageRep = NSBitmapImageRep.init(cgImage: checkImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
  233. let rWidth = resultImageRep.pixelsWide
  234. let rHeight = resultImageRep.pixelsHigh
  235. let rBitPerPixel = resultImageRep.bitsPerPixel / 8
  236. let rBytePerRow = resultImageRep.bytesPerRow
  237. let cWidth = checkImageRep.pixelsWide
  238. let cHeight = checkImageRep.pixelsHigh
  239. let cBitPerPixel = checkImageRep.bitsPerPixel / 8
  240. let cBytePerRow = checkImageRep.bytesPerRow
  241. let maxWidth = min(rWidth, cWidth) - 1
  242. let maxHeight = min(rHeight, cHeight) - 1
  243. // check background color
  244. // 挑选图片 对角斜线 上的相素进行识别
  245. var markInfo = NSMutableDictionary.init()
  246. for w in 0...maxWidth {
  247. let x = Int(w)
  248. for mark in 0...1 {
  249. let color = checkImageRep.colorAt(x: min(x, cWidth-1), y: min(mark == 0 ? x : (maxWidth - x), (cHeight-1))) as! NSColor
  250. let r = Int(color.redComponent*255)
  251. let g = Int(color.greenComponent*255)
  252. let b = Int(color.blueComponent*255)
  253. if (markInfo[color] != nil) {
  254. let count = (markInfo[color] as? NSNumber)!.intValue ?? 0
  255. markInfo[color] = NSNumber.init(value: count + 1)
  256. }else {
  257. markInfo[color] = NSNumber.init(value: 1)
  258. }
  259. }
  260. }
  261. var maxCount = Int(0);
  262. var bgColor : NSColor? = nil
  263. for color in markInfo.allKeys {
  264. let count = (markInfo[color] as? NSNumber)!.intValue ?? 0
  265. if count > maxCount {
  266. maxCount = count
  267. bgColor = color as! NSColor
  268. }
  269. }
  270. if nil != bgColor {
  271. NSLog(String("识别到背景色\(bgColor)"))
  272. }
  273. // Compare
  274. var equalCount = 0 as Double
  275. var bgCount = 0 as Double
  276. for w in 0...maxWidth {
  277. let x = Int(w)
  278. for h in 0...maxHeight {
  279. let y = Int(h)
  280. let cColor = checkImageRep.colorAt(x: x, y: y) as! NSColor
  281. let rColor = resultImageRep.colorAt(x: x, y: y) as! NSColor
  282. if (cColor.isEqual(to: rColor)) {
  283. equalCount = equalCount + 1
  284. if bgColor != nil &&
  285. cColor.isEqual(to: bgColor) {
  286. bgCount += 1
  287. }
  288. }
  289. }
  290. }
  291. NSLog(String("过滤点数目\(bgCount)"))
  292. return Double(max(equalCount-bgCount, 1)/(max(Double(cWidth) * Double(cHeight)-bgCount, 1)) * 100.0)
  293. }
  294. // Update Refrence image
  295. func canUpdateRefImage() -> Bool {
  296. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  297. let checkDirectory = self.checkFileDirectory()
  298. let originDirectory = self.originFileDirectory()
  299. let resultDirectory = self.resultFileDirectory()
  300. for fileName in files {
  301. let fName = NSString(string: fileName).deletingPathExtension
  302. let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  303. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  304. let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  305. var isDirectory = ObjCBool(false)
  306. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  307. // compare screenshoot between result file with check file
  308. let items = NSMutableArray()
  309. if (isDirectory.boolValue) {
  310. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  311. for item in NSArray(array: searchItems) {
  312. let ext = NSString(string: item as! String).pathExtension.lowercased()
  313. if NSArray(array: [_extention]).contains(ext) {
  314. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  315. items.add(fileName)
  316. }
  317. }
  318. }else {
  319. items.add(fName)
  320. }
  321. for item in items {
  322. let subFileName = item as! String
  323. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  324. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  325. if FileManager.default.fileExists(atPath: rComparePath) {
  326. if !FileManager.default.fileExists(atPath: cComparePath) {
  327. return true
  328. }
  329. let rfs = try! FileManager.default.attributesOfItem(atPath: rComparePath)[FileAttributeKey.size] as! NSNumber
  330. let cfs = try! FileManager.default.attributesOfItem(atPath: cComparePath)[FileAttributeKey.size] as! NSNumber
  331. if rfs.int64Value != cfs.int64Value {
  332. return true
  333. }
  334. }
  335. }
  336. }
  337. }
  338. return false
  339. }
  340. func updateRefImage() {
  341. let files = DataModel.shared.originFilesFor(_fileType, type: _type) as [String]
  342. for fileName in files {
  343. updateRefImage(fileName)
  344. }
  345. }
  346. func canUpdateRefImage(_ fileName:String) -> Bool {
  347. let checkDirectory = self.checkFileDirectory()
  348. let originDirectory = self.originFileDirectory()
  349. let resultDirectory = self.resultFileDirectory()
  350. let fName = NSString(string: fileName).deletingPathExtension
  351. let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  352. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  353. let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  354. var isDirectory = ObjCBool(false)
  355. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  356. // compare screenshoot between result file with check file
  357. let items = NSMutableArray()
  358. if (isDirectory.boolValue) {
  359. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  360. for item in NSArray(array: searchItems) {
  361. let ext = NSString(string: item as! String).pathExtension.lowercased()
  362. if NSArray(array: [_extention]).contains(ext) {
  363. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  364. items.add(fileName)
  365. }
  366. }
  367. }else {
  368. items.add(fName)
  369. }
  370. for item in items {
  371. let subFileName = item as! String
  372. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  373. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  374. if FileManager.default.fileExists(atPath: rComparePath) {
  375. if !FileManager.default.fileExists(atPath: cComparePath) {
  376. return true
  377. }
  378. let rfs = try! FileManager.default.attributesOfItem(atPath: rComparePath)[FileAttributeKey.size] as! NSNumber
  379. let cfs = try! FileManager.default.attributesOfItem(atPath: cComparePath)[FileAttributeKey.size] as! NSNumber
  380. if rfs.int64Value != cfs.int64Value {
  381. return true
  382. }
  383. }
  384. }
  385. }
  386. return false
  387. }
  388. func updateRefImage(_ fileName:String) {
  389. let checkDirectory = self.checkFileDirectory()
  390. let originDirectory = self.originFileDirectory()
  391. let resultDirectory = self.resultFileDirectory()
  392. let fName = NSString(string: fileName).deletingPathExtension
  393. let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  394. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
  395. let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  396. var isDirectory = ObjCBool(false)
  397. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  398. // compare screenshoot between result file with check file
  399. let items = NSMutableArray()
  400. if (isDirectory.boolValue) {
  401. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  402. for item in NSArray(array: searchItems) {
  403. let ext = NSString(string: item as! String).pathExtension.lowercased()
  404. if NSArray(array: [_extention]).contains(ext) {
  405. let fileName = NSString(string: fName+"."+_extention+"/\(item as! String)").deletingPathExtension
  406. items.add(fileName)
  407. }
  408. }
  409. }else {
  410. items.add(fName)
  411. }
  412. for item in items {
  413. let subFileName = item as! String
  414. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  415. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  416. if FileManager.default.fileExists(atPath: rComparePath) {
  417. if FileManager.default.fileExists(atPath: cComparePath) {
  418. try! FileManager.default.removeItem(atPath: cComparePath)
  419. }
  420. try! FileManager.default.createDirectory(atPath: NSString(string: cComparePath).deletingLastPathComponent,
  421. withIntermediateDirectories: true)
  422. try! FileManager.default.copyItem(atPath: rComparePath, toPath: cComparePath)
  423. }
  424. }
  425. }
  426. }
  427. /// Path
  428. func originFileDirectory() -> String {
  429. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kOriginPathComponent)")
  430. }
  431. func resultFileDirectory() -> String {
  432. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kResultPathComponent)")
  433. }
  434. func checkFileDirectory() -> String {
  435. return DataModel.shared.directoryPath().appendingFormat("/\(self.fileType())/\(self.type())/\(kCheckPathComponent)")
  436. }
  437. // check File Exist
  438. func hasOriginFile() -> Bool {
  439. if nil != self.originFileDirectory() && FileManager.default.fileExists(atPath: self.originFileDirectory()) {
  440. let files = try? FileManager.default.subpathsOfDirectory(atPath: self.originFileDirectory())
  441. return (files ?? []).count > 0
  442. }
  443. return false
  444. }
  445. func hasResultFile() -> Bool {
  446. if nil != self.resultFileDirectory() && FileManager.default.fileExists(atPath: self.resultFileDirectory()) {
  447. let files = try? FileManager.default.subpathsOfDirectory(atPath: self.resultFileDirectory())
  448. return (files ?? []).count > 0
  449. }
  450. return false
  451. }
  452. ///
  453. func clearCacheFiles() {
  454. reportString = NSMutableAttributedString.init();
  455. let resultDirectory = self.resultFileDirectory()
  456. var isDirectory = ObjCBool(false)
  457. if FileManager.default.fileExists(atPath: resultDirectory, isDirectory: &isDirectory) && isDirectory.boolValue {
  458. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultDirectory)
  459. for item in NSArray(array: searchItems) {
  460. let path = NSString(string: resultDirectory).appendingPathComponent(item as! String)
  461. if FileManager.default.fileExists(atPath: path) {
  462. try! FileManager.default.removeItem(atPath: path)
  463. }
  464. }
  465. }
  466. _degree = 0.0;
  467. _fileDegree.removeAll()
  468. }
  469. }
  470. extension AutoTest {
  471. func stringToImage(_ string:String) ->NSImage? {
  472. let length = Int(string.lengthOfBytes(using: .utf8)/2)
  473. var bytes = malloc(length)!
  474. for i in 0..<length {
  475. let index = i
  476. let subString = String(NSString(string: string).substring(with: NSMakeRange(Int(index * 2), 2)))
  477. bytes.storeBytes(of: UInt8(hexForString(subString)), as: UInt8.self)
  478. }
  479. var data = Data.init(bytes: bytes, count: length)
  480. let image = NSImage.init(data: data)
  481. return image
  482. }
  483. func hexForString(_ string:String) -> UInt8 {
  484. let chars = string.utf8CString
  485. if (string.lengthOfBytes(using: .utf8) >= 2) {
  486. return UInt8(intvalueForChar(chars[0]) * 16 + intvalueForChar(chars[1]))
  487. }
  488. return UInt8(0)
  489. }
  490. func intvalueForChar(_ char:CChar) -> UInt8 {
  491. let iValue = char
  492. // 0 ~ 9s
  493. if iValue >= 48 && iValue <= 57 {
  494. return UInt8(iValue - 48)
  495. }else if iValue >= 65 && iValue <= 70 {
  496. return UInt8(iValue - 65 + 10)
  497. }else if iValue >= 97 && iValue <= 102 {
  498. return UInt8(iValue - 97 + 10)
  499. }
  500. return UInt8(0)
  501. }
  502. }