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

  • (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字节数组)