美文网首页
字符串类型转Data

字符串类型转Data

作者: 开洋_shen | 来源:发表于2017-07-03 15:51 被阅读0次

 - (NSData *)dataWithHexString:(NSString *)hexString {

NSInteger len = [hexString length];

char *myBuffer = (char *)malloc(len / 2 + 1);

bzero(myBuffer, len / 2 + 1);

for (int i = 0; i < len - 1; i += 2) {

unsigned int anInt;

NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];

NSScanner * scanner = [NSScanner scannerWithString:hexCharStr] ;

[scanner scanHexInt:&anInt];

myBuffer[i / 2] = (char)anInt;

}

NSData *hexData = [[NSData alloc] initWithBytes:myBuffer length:len/2];

free(myBuffer);

return hexData;

}

相关文章

网友评论

      本文标题:字符串类型转Data

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