iOS 之NSData与NSString,Byte数组,UIIm
近期与服务器的Socket
的交互中用到了底层的字节数组Byte
的发送与接收,写下关于oc
中的不同类型之间的转化,以供参考:
1.NSData与NSString
NSData->NSString:
NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
NSString->NSData:
NSString *aString = @"1234abcd";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
2.NSData与Byte数组
NSData->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];
}
NSLog(@"bytes 的16进制数为:%@",hexStr);
16进制数->Byte数组:
//将16进制数据转化成Byte 数组
NSString hexString = @"3e435fab9c34891f"; //16进制字符串
int j=0;
Byte bytes[128];
///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:128];
NSLog(@"newData=%@",newData);
UIImage-> NSData
NSData *imageData = UIImagePNGRepresentation(aimae);
分享自己的写的函数,16进制
转data
类型直接调用就好了:
(NSData )strEndMinute:(NSString )endMinute {
int j=0;
Byte bytes[endMinute.length / 2];
for(int i=0;i<[endMinute length];i++)
{
int int_ch; /// 两位16进制数转化后的10进制数
unichar hex_char1 = [endMinute 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 = [endMinute 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;
bytes[j] = int_ch; ///将转化后的数放入Byte数组里
j++;
}
NSData *newData = [[NSData alloc] initWithBytes:bytes length:endMinute.length / 2];
return newData;
}
字符串转化为16进制
-(NSString *)convertStringToHexStr:(NSString *)str {
if (!str || [str length] == 0) {
return @"";
}
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
unsigned char dataBytes = (unsigned char)bytes;
for (NSInteger i = 0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr];
}
}
}];
return string;
}