十六进制字符串NSString转换为NSData

下面提供的是一个NSString的Category

下载Category代码

.h文件如下,具体实现在下面一个代码块中

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import <Foundation/Foundation.h>
@interface NSString (Trans)
/**
* 十六 进制字符串转换为 data
* 24211D3498FF62AF --> <24211D34 98FF62AF>
*
* @param str 要转换的字符串
*
* @return 转换后的数据
*/
+ (NSData*)hexToBytes:(NSString *)str;
/**
* data 转换为十六进制字符串
* <24211D34 98FF62AF> --> 24211D3498FF62AF
*
* @param data 要转换的data
*
* @return 转换后的字符串
*/
+ (NSString *)hexStringFromData:(NSData *)data;
/**
* 由byte转为字符串
*
* @param byteVal byte
*
* @return
*/
+ (NSString *)stringFromByte:(Byte)byteVal;
/**
* hex字符串转为ASC码 00 --> 3030
*
* @param hex hex字符串
*
* @return 转码后的ASC字符串
*/
+ (NSString *)hexToAsc:(NSString *)hex;
/**
* ASC码转为Hex字符串 3030 --> 00
*
* @param asc ASC字符串
*
* @return 转码后的Hex字符串
*/
+ (NSString *)ascToHex:(NSString *)asc;
/**
* 十六进制字符串转二进制字符串
*
* @param hex 十六进制字符串
*
* @return 二进制字符串
*/
+(NSString *)HexToBinary:(NSString *)hex;
/**
* 2进制字符串转16进制字符串,如 11110011 -> F3
*
* @param Binary 二进制字符串
*
* @return 16进制字符串
*/
+(NSString *)BinaryToHex:(NSString *)Binary;
@end

.m文件如下

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#import "NSString+Trans.h"****
@implementation NSString (Trans)
+(NSData*)hexToBytes:(NSString *)str{
NSMutableData* data = [NSMutableData data];
int idx;
for (idx = 0; idx+2 <= str.length; idx+=2) {
NSRange range = NSMakeRange(idx, 2);
NSString* hexStr = [str substringWithRange:range];
NSScanner* scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
[scanner scanHexInt:&intValue];
[data appendBytes:&intValue length:1];
}
return data;
}
+ (NSString *)hexStringFromData:(NSData *)data{
NSMutableString *str = [NSMutableString string];
Byte *byte = (Byte *)[data bytes];
for (int i = 0; i<[data length]; i++) {
// byte+i为指针
[str appendString:[self stringFromByte:*(byte+i)]];
}
return str;
}
+ (NSString *)stringFromByte:(Byte)byteVal{
NSMutableString *str = [NSMutableString string];
//取高四位
Byte byte1 = byteVal>>4;
//取低四位
Byte byte2 = byteVal & 0xf;
//拼接16进制字符串
[str appendFormat:@"%x",byte1];
[str appendFormat:@"%x",byte2];
return str;
}
/**
* hex字符串转为ASC码 00 --> 3030
*
* @param hex hex字符串
*
* @return 转码后的ASC字符串
*/
+ (NSString *)hexToAsc:(NSString *)hex{
char szData[1024]={0};
const char *pBytes = [hex UTF8String];
if(NULL != pBytes)
{
for(int i=0; i<hex.length; i++)
{
char tmp[16];
sprintf(tmp, "%0.2X",pBytes[i]);
strcat(szData, tmp);
}
return [NSString stringWithFormat:@"%s",szData];
}
return nil;
}
/**
* ASC吗转为Hex字符串 3030 --> 00
*
* @param asc ASC字符串
*
* @return 转码后的Hex字符串
*/
+ (NSString *)ascToHex:(NSString *)asc{
char szData[1024]={0};
const char *ascBytes = [asc UTF8String];
if (NULL != ascBytes) {
[self ascToHex:ascBytes len:asc.length outAscii:szData];
return [NSString stringWithFormat:@"%s",szData];
}
return nil;
}
+ (void)ascToHex:(const char *)hex len:(NSUInteger)length outAscii:(char *)ascii
{
for (int i = 0; i < length; i += 2)
{
if (hex[i] >= '0' && hex[i] <= '9')
ascii[i / 2] = (hex[i] - '0') << 4;
else if (hex[i] >= 'a' && hex[i] <= 'z')
ascii[i / 2] = (hex[i] - 'a' + 10) << 4;
else if (hex[i] >= 'A' && hex[i] <= 'Z')
ascii[i / 2] = (hex[i] - 'A' + 10) << 4;
if (hex[i + 1] >= '0' && hex[i + 1] <= '9')
ascii[i / 2] += hex[i + 1] - '0';
else if (hex[i + 1] >= 'a' && hex[i + 1] <= 'z')
ascii[i / 2] += hex[i + 1] - 'a' + 10;
else if (hex[i + 1] >= 'A' && hex[i + 1] <= 'Z')
ascii[i / 2] += hex[i + 1] - 'A' + 10;
}
}
/**
* 十六进制字符串转二进制字符串
*
* @param hex 十六进制字符串
*
* @return 二进制字符串
*/
+(NSString *)HexToBinary:(NSString *)hex
{
NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] init];
hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
[hexDic setObject:@"0000" forKey:@"0"];
[hexDic setObject:@"0001" forKey:@"1"];
[hexDic setObject:@"0010" forKey:@"2"];
[hexDic setObject:@"0011" forKey:@"3"];
[hexDic setObject:@"0100" forKey:@"4"];
[hexDic setObject:@"0101" forKey:@"5"];
[hexDic setObject:@"0110" forKey:@"6"];
[hexDic setObject:@"0111" forKey:@"7"];
[hexDic setObject:@"1000" forKey:@"8"];
[hexDic setObject:@"1001" forKey:@"9"];
[hexDic setObject:@"1010" forKey:@"A"];
[hexDic setObject:@"1011" forKey:@"B"];
[hexDic setObject:@"1100" forKey:@"C"];
[hexDic setObject:@"1101" forKey:@"D"];
[hexDic setObject:@"1110" forKey:@"E"];
[hexDic setObject:@"1111" forKey:@"F"];
NSMutableString *binaryString=[[NSMutableString alloc] init];
for (int i=0; i<[hex length]; i++) {
NSString *key = [hex substringWithRange:NSMakeRange(i, 1)];
[binaryString appendString:[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];
}
return binaryString;
}
/**
* 2进制字符串转16进制字符串,如 11110011 -> F3
*
* @param Binary 二进制字符串
*
* @return 16进制字符串
*/
+(NSString *)BinaryToHex:(NSString *)Binary
{
if ([Binary length]%4 == 0) {
NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] init];
hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
[hexDic setObject:@"0" forKey: @"0000"];
[hexDic setObject:@"1" forKey: @"0001"];
[hexDic setObject:@"2" forKey: @"0010"];
[hexDic setObject:@"3" forKey: @"0011"];
[hexDic setObject:@"4" forKey: @"0100"];
[hexDic setObject:@"5" forKey: @"0101"];
[hexDic setObject:@"6" forKey: @"0110"];
[hexDic setObject:@"7" forKey: @"0111"];
[hexDic setObject:@"8" forKey: @"1000"];
[hexDic setObject:@"9" forKey: @"1001"];
[hexDic setObject:@"A" forKey: @"1010"];
[hexDic setObject:@"B" forKey: @"1011"];
[hexDic setObject:@"C" forKey: @"1100"];
[hexDic setObject:@"D" forKey: @"1101"];
[hexDic setObject:@"E" forKey: @"1110"];
[hexDic setObject:@"F" forKey: @"1111"];
NSMutableString *hexString=[[NSMutableString alloc] init];
for (int i=0; i<[Binary length]/4; i++) {
NSString *key = [Binary substringWithRange:NSMakeRange(4*i, 4)];
// hexString = [NSString stringWithFormat:@"%@%@",binaryString,[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];
[hexString appendString:[NSString stringWithFormat:@"%@",[hexDic objectForKey:key]]];
}
return hexString;
}else
return nil;
}
@end