遍历NSData的方法
// 第一种方法
NSMutableString* resultAsHexBytes = [NSMutableString string];
NSString *string = @"abc";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[data enumerateByteRangesUsingBlock:^(const void *bytes,
NSRange byteRange,
BOOL *stop) {
// To print raw byte values as hex
for (NSUInteger i = 0; i < byteRange.length; ++i) {
[resultAsHexBytes appendFormat:@"%02x", ((uint8_t*)bytes)[i]];
}
NSLog(@"resultAsHexBytes:%@", resultAsHexBytes);
}];
// 第二种方法
NSMutableString *result = [NSMutableString string];
const char *bytes = [data bytes];
for (int i = 0; i < [data length]; i++)
{
[result appendFormat:@"%02hhx", (unsigned char)bytes[i]];
}
NSLog(@"result:%@", result);
输出结果分别是:
result:616263
resultAsHexBytes:616263
值得注意的是,第一种方法只适用于iOS7之后的系统。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: 【常用网站】常用的网址列表
- 下一篇:没有了