AlgorithmTool.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // AlgorithmTool.m
  3. // OYGameSKD
  4. //
  5. // Created by duowan123 on 2021/4/27.
  6. // Copyright © 2021 jiketeam. All rights reserved.
  7. //
  8. #import "AlgorithmTool.h"
  9. @implementation AlgorithmTool
  10. //优先计算校验位--无误再进行数据处理 数据前面所有字节数相加是否等于最后一位
  11. +(BOOL)verificationRusult:(NSData*)characteristicValue{
  12. if (characteristicValue.length<3){
  13. NSLog(@"无效报文");
  14. return NO;
  15. }
  16. NSData * frontData = [characteristicValue subdataWithRange:NSMakeRange(0, characteristicValue.length-1)];
  17. NSData * behindData = [characteristicValue subdataWithRange:NSMakeRange(characteristicValue.length-1, 1)];
  18. Byte frontBytes = [self byteSumBBC:frontData];
  19. Byte behindByte = [self byteSumBBC:behindData];
  20. if (frontBytes == behindByte){//校验位计算结果
  21. return YES;
  22. }else{
  23. NSLog(@"计算校验位出错");
  24. return NO;
  25. }
  26. }
  27. //测试
  28. + (Byte)bbcByte:(Byte *)bytes{
  29. Byte bcc = 0;
  30. NSInteger byteLength = sizeof(bytes);
  31. for (int i = 0; i < byteLength; i++){
  32. bcc += bytes[i];
  33. }
  34. return bcc;
  35. }
  36. //bbc
  37. + (Byte)byteSumBBC:(NSData *)data{
  38. Byte bcc = 0;
  39. if (data.length == 0){
  40. return bcc;
  41. }
  42. Byte *bytes = (Byte *)[data bytes];
  43. for (int i = 0; i < data.length; i++){
  44. bcc += bytes[i];
  45. // UInt8 ins = bytes[i];
  46. // NSLog(@"bytes[i] = %x",bytes[i]);
  47. // NSLog(@"bcc = %x",bcc);
  48. }
  49. // NSLog(@"bccTotavttelValue = %x",bcc);
  50. return bcc;
  51. }
  52. //Data To Char:返回1Byte的char数据 只有一个字节不会数据溢出 不用大小端转换
  53. + (char)dataToChar:(NSData *)data{
  54. char b = 0;
  55. //1个字节
  56. [data getBytes:&b length:data.length];
  57. return b;
  58. }
  59. //Data To Short:返回2Byte的short数据 数据溢出要大小端转换
  60. +(short)dataToSwapBigShortToHost:(NSData*)data andRange:(NSRange)range{
  61. //两个字节
  62. short a = 0;
  63. [data getBytes:&a range:range];
  64. a = NSSwapBigShortToHost(a);
  65. return a;
  66. }
  67. //Data To Int:返回 不定个数 Byte的int数据
  68. +(int)dataToInt:(NSData*)data{
  69. //2或4个字节
  70. int a = 0;
  71. [data getBytes:&a length:data.length];
  72. return a;
  73. }
  74. //Data To Int:返回 不定个数 Byte的int数据 数据溢出要大小端转换
  75. +(int)dataToSwapBigIntToHost:(NSData*)data andRange:(NSRange)range{
  76. //四个字节
  77. int a = 0;
  78. [data getBytes:&a range:range];
  79. a = NSSwapBigIntToHost(a);
  80. return a;
  81. }
  82. /**
  83. 十六进制数据流转字符串
  84. */
  85. +(NSString *)hexadecimalString:(NSData *)data{
  86. NSString *result;
  87. const unsigned char *dataBuffer = (const unsigned char *)[data bytes];
  88. if (!dataBuffer) {
  89. return nil;
  90. }
  91. NSUInteger dataLength = [data length];
  92. NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
  93. for (int i = 0; i<dataLength; i++) {
  94. //02x 表示两个位置 显示的16进制
  95. [hexString appendString:[NSString stringWithFormat:@"%02lx",(unsigned long)dataBuffer[i]]];
  96. }
  97. result = [NSString stringWithString:hexString];
  98. return result;
  99. }
  100. /**
  101. 字符串转16进制NSData数据流
  102. */
  103. +(NSData *)convertHexStrToData:(NSString *)str{
  104. if (!str || [str length] == 0){
  105. return nil;
  106. }
  107. NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
  108. NSRange range;
  109. if ([str length] % 2 == 0) {
  110. range = NSMakeRange(0, 2);
  111. } else {
  112. range = NSMakeRange(0, 1);
  113. }
  114. for (NSInteger i = range.location; i < [str length]; i += 2){
  115. unsigned int anInt;
  116. NSString *hexCharStr = [str substringWithRange:range];
  117. NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
  118. // NSLog(@"字符串转16进制数据流 scanner: %@", scanner);
  119. [scanner scanHexInt:&anInt];
  120. NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
  121. [hexData appendData:entity];
  122. range.location += range.length;
  123. range.length = 2;
  124. }
  125. // NSLog(@"字符串转16进制数据流 hexdata: %@", hexData);
  126. return hexData;
  127. }
  128. //json字符串转字典
  129. + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
  130. if (jsonString == nil){
  131. return nil;
  132. }
  133. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  134. NSError *err;
  135. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
  136. if(err){
  137. NSLog(@"json解析失败:%@",err);
  138. return nil;
  139. }
  140. return dic;
  141. }
  142. @end