-
Notifications
You must be signed in to change notification settings - Fork 0
/
JWBlueDataChangeTool.m
214 lines (160 loc) · 5.18 KB
/
JWBlueDataChangeTool.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// JWBlueDataChangeTool.m
// SmartAudio
//
// Created by 陈经纬 on 2018/4/8.
// Copyright © 2018年 new4545. All rights reserved.
//
#import "JWBlueDataChangeTool.h"
@implementation JWBlueDataChangeTool
/**
将传入的NSData类型转换成NSString并返回
*/
+ (NSString*)hexadecimalString:(NSData *)data
{
NSString* result;
const unsigned char* dataBuffer = (const unsigned char*)[data bytes];
if(!dataBuffer){
return nil;
}
NSUInteger dataLength = [data length];
NSMutableString* hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for(int i = 0; i < dataLength; i++){
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
}
result = [NSString stringWithString:hexString];
return result;
}
/**
将传入的NSString(10进制)类型转换成NSData并返回(格式: xxxxxxxx)
*/
+ (NSData*)dataWithIntString:(NSString *)intString{
NSMutableData *data = [NSMutableData data];
int idx;
for(idx = 0; idx + 2 <= intString.length; idx += 2){
NSRange range = NSMakeRange(idx, 2);
NSInteger i = [intString substringWithRange:range].integerValue;
NSData *tempData = [FGDataChangeTool dataWithInt:i andLength:1];
[data appendData:tempData];
}
return data;
}
/**
将传入的NSString(16进制)类型转换成NSData并返回(格式: xxxxxxxx)
*/
+ (NSData*)dataWithHexstring:(NSString *)hexstring
{
NSMutableData *data = [NSMutableData data];
int idx;
for(idx = 0; idx + 2 <= hexstring.length; idx += 2){
NSRange range = NSMakeRange(idx, 2);
NSString* hexStr = [hexstring substringWithRange:range];
NSScanner* scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
[scanner scanHexInt:&intValue];
[data appendBytes:&intValue length:1];
}
return data;
}
/**
字符串以ASCII编码格式解码
@param ASCIIString 45312e30
@return 字符串
*/
+ (NSString *)stringWithASCII:(NSString *)ASCIIString{
NSMutableString *str = [NSMutableString string];
for (int i = 0; i + 2 <= (ASCIIString.length) ; i +=2 ) {
NSString *subStr = [ASCIIString substringWithRange:NSMakeRange(i, 2)];
NSString *ascc = NSStringFormat(@"%c",[FGDataChangeTool numberWithHexString:subStr]);
[str appendString:ascc];
}
return str;
}
/**
删除字符串里面的某个字符
@param string <#string description#>
@param str <#str description#>
@return <#return value description#>
*/
+ (NSString *)deleteString:(NSString *)string withDeletStr:(NSString *)str{
NSMutableString *dataStr = [NSMutableString string];
NSArray *arry = [string componentsSeparatedByString:str];
for (int i = 0; i<arry.count; i++) {
NSString *str = arry[i];
[dataStr appendString:str];
}
return dataStr;
}
+ (NSData *)dataWithInt:(NSInteger)integer andLength:(int)len{
Byte abyte[len];
if (len == 1) {
abyte[0] = (Byte) (0xff & integer);
} else if (len == 2) {
abyte[0] = (Byte) ((0xff00 & integer) >> 8);
abyte[1] = (Byte) (0xff & integer);
} else {
abyte[0] = (Byte) ((0xff000000 & integer) >> 24);
abyte[1] = (Byte) ((0xff0000 & integer) >> 16);
abyte[2] = (Byte) ((0xff00 & integer) >> 8);
abyte[3] = (Byte) (0xff & integer);
}
NSData *adata = [NSData dataWithBytes:abyte length:len];
return adata;
}
#pragma mark - 进制转化
/**
十六进制转十进制
*/
+ (NSInteger)numberWithHexString:(NSString *)hexString{
const char *hexChar = [hexString cStringUsingEncoding:NSUTF8StringEncoding];
NSInteger hexNumber;
sscanf(hexChar, "%x", &hexNumber);
return hexNumber;
}
/**二进制转十进制*/
+ (NSString *)toDecimalSystemWithBinarySystem:(NSString *)binary
{
int ll = 0 ;
int temp = 0 ;
for (int i = 0; i < binary.length; i ++)
{
temp = [[binary substringWithRange:NSMakeRange(i, 1)] intValue];
temp = temp * powf(2, binary.length - i - 1);
ll += temp;
}
NSString * result = [NSString stringWithFormat:@"%d",ll];
return result;
}
//将十进制转化为十六进制
+ (NSString *)ToHex:(int)tmpid
{
NSString *nLetterValue;
NSString *str =@"";
long long int ttmpig;
for (int i = 0; i<9; i++) {
ttmpig=tmpid%16;
tmpid=tmpid/16;
switch (ttmpig)
{
case 10:
nLetterValue =@"a";break;
case 11:
nLetterValue =@"b";break;
case 12:
nLetterValue =@"c";break;
case 13:
nLetterValue =@"d";break;
case 14:
nLetterValue =@"e";break;
case 15:
nLetterValue =@"f";break;
default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
}
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}
}
return str;
}
@end