开发过程中总会转换各种数据类型,很多人直接把常用的数据类型封装后使用,时间长了反而想不起来各个数据类型之间是如何转换的,在这里就做一个简单的总结,以备不时之需.
NSString和NSData之间的相互转换
NSString 和 NSData之间的转换频率还是挺高的,尤其是JSON字符串和字典之间转换会用到.
1
2
3
4
5
6
7
|
NSString *string = @"iOS常用数据类型转换";
NSData *data = [string dataUsingEncoding: NSUTF8StringEncoding];
NSString *stringByData = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
|
NSDictionary,NSData,JSON字符串之间的相互转换
在OC中,NSDictionary,NSData,JSON字符串三者之间相爱相杀多年,写者伤心,转者流泪.
NSDictionary -> NSData:
1
2
3
4
5
6
7
8
9
10
|
NSDictionary *dic = @{ @"人" : @"小明",
@"身高" : @"175",
@"年龄" : @"25" };
NSError *err = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dic
options:NSJSONWritingPrettyPrinted
error:err];
if(err)
NSLog(@"%@", [err description]);
|
NSData -> NSDictionary:
1
2
3
4
5
6
|
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:err];
if(err)
NSLog(@"%@", [err description]);
|
NSDictionary -> JSON字符串(用原生API只能先转成NSData再转成JSON字符串):
1
2
3
4
5
6
7
8
9
10
11
12
|
NSDictionary *dic = @{ @"人" : @"小明",
@"身高" : @"175",
@"年龄" : @"25" };
NSData *data = [NSJSONSerialization dataWithJSONObject:dic
options:NSJSONWritingPrettyPrinted
error:err];
if(err)
NSLog(@"%@", [err description]);
NSString *JSONStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
|
JSON字符串 -> NSDictionary(同样需要先转NSData):
1
2
3
4
5
6
7
8
9
10
|
NSString *JSONStr = @"{\n \"175\" : \"身高\",\n \"小明\" : \"人\",\n \"25\" : \"年龄\"\n}";
NSData *data = [JSONStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:err];
if(err)
NSLog(@"%@", [err description]);
|
C和OC转换字符串
C语言转换成OC字符串
1
|
NSString *ocStr = [NSString stringWithUTF8String:cStr];
|
OC转换成C语言字符串
1
|
char *cStr = ocStr.UTF8String;
|
NSData 和 Byte 之间的转换
这两者之间的转换平时不是很常用,不过还是需要了解一下的.
NSData -> Byte 数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
NSString *numberString = @"1234567890";
NSData *numberData = [numberString dataUsingEncoding: NSUTF8StringEncoding];
Byte *numberByte = (Byte *)[numberData bytes];
for(int i=0;i<[numberData length];i++)
{
printf("numberByte = %d\n",numberByte[i]);
}
NSString *characterString = @"abcdefghijklmnopqrstuvwxyz";
NSData *characterData = [characterString dataUsingEncoding: NSUTF8StringEncoding];
Byte *characterByte = (Byte *)[characterData bytes];
for(int i=0;i<[characterData length];i++)
{
printf("numberByte = %d\n",characterByte[i]);
}
|
Byte 数组 -> NSData
1
2
|
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,24,25,26};
NSData *data = [[NSData alloc] initWithBytes:byte length:27];
|
Byte数组 -> 16进制数
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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,24,25,26};
NSData *data = [[NSData alloc] initWithBytes:byte length:27];
Byte *bytes = (Byte *)[data bytes];
NSString *hexStr=@"";
for(int i=0;i<[data 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数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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);
|
NSData 与 UIImage
NSData -> UIImage
1
2
3
4
5
6
7
8
9
|
UIImage *aimage = [UIImage imageWithData: imageData];
//例:从本地文件沙盒中取图片并转换为NSData
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *name = [NSString stringWithFormat:@"ceshi.png"];
NSString *finalPath = [path stringByAppendingPathComponent:name];
NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
UIImage *aimage = [UIImage imageWithData: imageData];
|
UIImage -> NSData
1
2
3
4
5
6
7
8
9
|
NSData *imageData = UIImagePNGRepresentation(aimae);
// 图片压缩
// 压缩成JPEG各式
// 1.0表示无损压缩,小于1.0表示有损
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
// 压缩成PNG各式
NSData *imageData = UIImagePNGRepresentation(image);
|
小技巧: 如何用一行代码计算NSString字符的个数?(peak大神在公众号上问的问题)
1
|
[str lengthOfBytesUsingEncoding:NSUTF32StringEncoding]/4
|
当然还有其他方式,但绝不是str.length。 length返回的是以utf16为单位的code unit个数。像很多emoji表情都会占2个unit,实际却是一个字符。