- (NSData*)dataFormHexString:(NSString*)hexString{
hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
if (!(hexString && [hexString length] > 0 && [hexString length]%2 == 0)) {
return nil;
}
Byte tempbyt[1]={0};
NSMutableData* bytes=[NSMutableData data];
for(int i=0;i<[hexString length];i++)
{
unichar hex_char1 = [hexString characterAtIndex:i]; 两位16进制数中的第一位(高位*16)
int int_ch1;
if(hex_char1 >= '0' && hex_char1 <='9')
int_ch1 = (hex_char1-48)*16; 0 的Ascll - 48
else if(hex_char1 >= 'A' && hex_char1 <='F')
int_ch1 = (hex_char1-55)*16; A 的Ascll - 65
else
return nil;
i++;
unichar hex_char2 = [hexString
16进制NSString与NSData互转
最新推荐文章于 2018-02-04 12:16:49 发布
本文介绍了如何在iOS开发中实现16进制字符串与NSData对象之间的相互转换。通过提供的两个方法,可以将NSString转换为NSData,反之亦然。转换过程中涉及到对16进制字符的解析和字节拼接。
摘要由CSDN通过智能技术生成