把16進制字符串轉換成NSData:
-(NSData *)hexString:(NSString *)hexString {
int j=0;
Byte bytes[20];
///3ds key的Byte 數組, 128位
for(int i=0; i<[hexString length]; i++)
{
int int_ch; /// 兩位16進制數轉化後的10進制數
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
int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
i++;
unichar hex_char2 = [hexString characterAtIndex:i]; ///兩位16進制數中的第二位(低位)
int int_ch2;
if(hex_char2 >= '0' && hex_char2 <='9')
int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
else if(hex_char1 >= 'A' && hex_char1 <='F')
int_ch2 = hex_char2-55; //// A 的Ascll - 65
else
int_ch2 = hex_char2-87; //// a 的Ascll - 97
int_ch = int_ch1+int_ch2;
NSLog(@"int_ch=%d",int_ch);
bytes[j] = int_ch; ///將轉化後的數放入Byte數組裏
j++;
}
NSData *newData = [[NSData alloc] initWithBytes:bytes length:20];
return newData;
}
把Byte轉換成NSData:
Byte byte[] = {1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
NSData *sData=[NSData dataWithBytes:byte length:10];