十六进制字符串 转 byte数组
2019-02-16 本文已影响0人
RWz_my
目标字符串 NSString *str = @"0200107580FD7590FD75A0FD75B0FD22787FE4F6D8FD7581070200030200107580FD7590FD75A0FD";
转换后
byte数组 Byte byteArr[] = { 0x02, 0x00, 0x10, ... , 0xFD };
调用方法:NSData *temp = [string hexToBytes];
//十六进制字符串转换成字节数组
- (NSData *)hexToBytes
{
NSMutableData *muData = [NSMutableData data];
for (int i = 0; i + 2 <= self.length; i += 2)
{
NSRange range = NSMakeRange(i, 2);
NSString *hexStr = [self substringWithRange:range];
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
[scanner scanHexInt:&intValue];
[muData appendBytes:&intValue length:1];
}
return muData;
}