16进制字符串转换为NSData,然后转换为NSString---用于安全支付环境
+(NSData *)hexToByteToNSData:(NSString *)str { int j=0; Byte bytes[[str length]/2]; for(int i=0;i<[str length];i++) { int int_ch; ///两位16进制数转化后的10进制数 unichar hex_char1 = [str 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 = [str characterAtIndex:i]; ///两位16进制数中的第二位(低位) int int_ch2; if(hex_char2 >= '0' && hex_char2 <='9') int_ch2 = (hex_char2-48); //// 0 的Ascll - 48 else if(hex_char2 >= 'A' && hex_char2 <='F') int_ch2 = hex_char2-55; //// A 的Ascll - 65 else int_ch2 = hex_char2-87; //// a 的Ascll - 97 int_ch = int_ch1+int_ch2; bytes[j] = int_ch; ///将转化后的数放入Byte数组里 // if (j==[str length]/2-2) { // int k=2; // int_ch=bytes[0]^bytes[1]; // while (k // int_ch=int_ch^bytes[k]; // k++; // } // bytes[j] = int_ch; // } j++; } NSData *newData = [[NSData alloc] initWithBytes:bytes length:[str length]/2 ]; NSLog(@"%@",newData); return newData; }
时间: 2024-12-13 00:58:35