NSdata 与 Byte数组相互转换

NSData 与 ByteNSData-> Byte数组

NSString *testString = @"1234567890";

NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];

Byte *testByte = (Byte *)[testData bytes];

for(int i=0;i<[testData length];i++)

printf("testByte = %d\n",testByte[i]);

Byte数组-> NSData

Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};

NSData *adata = [[NSData alloc] initWithBytes:byte length:24];

Byte数组->16进制数

Byte *bytes = (Byte *)[aData bytes];

NSString *hexStr=@"";

for(int i=0;i<[encryData length];i++){

    NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数

    if([newHexStr length]==1){

        hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];

    }else {

        hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];

    }

}

你可能感兴趣的:(NSdata 与 Byte数组相互转换)