收集的一些常用的转换。

View Code
  1 . NSData 与 NSString
2
3 NSData-> NSString
4
5 NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];
6
7
8
9 NSString->NSData
10
11 NSString *aString = @"1234abcd";
12
13 NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
14 .NSData 与 Byte
15
16 NSData-> Byte数组
17
18 NSString *testString = @"1234567890";
19
20 NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
21
22 Byte *testByte = (Byte *)[testData bytes];
23
24 for(int i=0;i<[testData length];i++)
25
26 printf("testByte = %d\n",testByte[i]);
27
28
29
30 Byte数组-> NSData
31
32 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};
33
34 NSData *adata = [[NSData alloc] initWithBytes:byte length:24];
35
36
37
38 Byte数组->16进制数
39
40 Byte *bytes = (Byte *)[aData bytes];
41
42 NSString *hexStr=@"";
43
44 for(int i=0;i<[encryData length];i++)
45
46 {
47
48 NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
49
50 if([newHexStr length]==1)
51
52 hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
53
54 else
55
56 hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
57
58 }
59
60 NSLog(@"bytes 的16进制数为:%@",hexStr);
61 进制数->Byte数组
62
63 ///// 将16进制数据转化成Byte 数组
64
65 NSString *hexString = @"3e435fab9c34891f"; //16进制字符串
66
67 int j=0;
68
69 Byte bytes[128]; ///3ds key的Byte 数组, 128位
70
71 for(int i=0;i<[hexString length];i++)
72
73 {
74
75 int int_ch; /// 两位16进制数转化后的10进制数
76
77
78
79 unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
80
81 int int_ch1;
82
83 if(hex_char1 >= '0' && hex_char1 <='9')
84
85 int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48
86
87 else if(hex_char1 >= 'A' && hex_char1 <='F')
88
89 int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
90
91 else
92
93 int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
94
95 i++;
96
97
98
99 unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
100
101 int int_ch2;
102
103 if(hex_char2 >= '0' && hex_char2 <='9')
104
105 int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
106
107 else if(hex_char1 >= 'A' && hex_char1 <='F')
108
109 int_ch2 = hex_char2-55; //// A 的Ascll - 65
110
111 else
112
113 int_ch2 = hex_char2-87; //// a 的Ascll - 97
114
115
116
117 int_ch = int_ch1+int_ch2;
118
119 NSLog(@"int_ch=%d",int_ch);
120
121 bytes[j] = int_ch; ///将转化后的数放入Byte数组里
122
123 j++;
124
125 }
126
127 NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];
128
129 NSLog(@"newData=%@",newData);
130 . NSData 与 UIImage
131
132 NSData->UIImage
133
134 UIImage *aimage = [UIImage imageWithData: imageData];
135
136
137
138 //例:从本地文件沙盒中取图片并转换为NSData
139
140 NSString *path = [[NSBundle mainBundle] bundlePath];
141
142 NSString *name = [NSString stringWithFormat:@"ceshi.png"];
143
144 NSString *finalPath = [path stringByAppendingPathComponent:name];
145
146 NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
147
148 UIImage *aimage = [UIImage imageWithData: imageData];
149
150
151
152 UIImage-> NSData
153
154 NSData *imageData = UIImagePNGRepresentation(aimae);



posted on 2012-02-09 23:53  AXZON  阅读(193)  评论(0编辑  收藏  举报

导航