美文网首页
iOS 蓝牙数据如何转化为UInt8字节数组

iOS 蓝牙数据如何转化为UInt8字节数组

作者: 可乐小子 | 来源:发表于2021-11-26 16:47 被阅读0次
  • (void)parseDataAndEnqueue:(NSMutableArray *)data {

    int dataSize = data.count;
    if (dataSize <= 0) {
    return ;
    }

    id readBuffer = [data mutableCopy];
    NSString *newStr = [readBuffer[0] hexval];
    NSMutableArray *temps = [NSMutableArray array];
    for(int i =0; i < [newStr length]; i = i + 2) {
    [temps addObject:[newStr substringWithRange:NSMakeRange(i,2)]];
    }

NSMutableArray  *tempsArr = [NSMutableArray  array];
for (NSString  *lxStr in temps) {
    NSNumber  *number = @( strtoul(lxStr.UTF8String, 0, 16));//16进制转10进制
    [tempsArr   addObject:number];
}
 
NSMutableArray  *acks = [NSMutableArray  arrayWithArray:[self.mSDUParser   parseWithBuffer:tempsArr]];

}

import "NSData+HexDump.h"

@implementation NSData (HexDump)

  • (NSString *)hexval
    {
    NSMutableString *hex = [NSMutableString string];
    unsigned char *bytes = (unsigned char *)[self bytes];
    char temp[3];
    int i = 0;

    for (i = 0; i < [self length]; i++) {
    temp[0] = temp[1] = temp[2] = 0;
    (void)sprintf(temp, "%02x", bytes[i]);
    [hex appendString:[NSString stringWithUTF8String:temp]];
    }

    return hex;
    }

}

相关文章

网友评论

      本文标题:iOS 蓝牙数据如何转化为UInt8字节数组

      本文链接:https://www.haomeiwen.com/subject/zhlxxrtx.html