近期做了蓝牙通信这一块,中间遇到了许多数据之间的进制转换、数据类型的转换,在这里记录一下,也给大家分享一下。
我自己建了一个NSString的类别(Category),把这些方法放进去,用的时候就方便多了。
16进制字符串转换成NSData数据类型
#pragma mark - 16进制字符串转data
+ (NSData *)stringToHexData:(NSString *)hexStr
{
unsigned long len = [hexStr length] / 2; // Target length
unsigned char *buf = malloc(len);
unsigned char *whole_byte = buf;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [hexStr length] / 2; i++) {
byte_chars[0] = [hexStr characterAtIndex:i*2];
byte_chars[1] = [hexStr characterAtIndex:i*2+1];
*whole_byte = strtol(byte_chars, NULL, 16);
whole_byte++;
}
NSData *data = [NSData dataWithBytes:buf length:len];
free( buf );
return data;
}
NSData数据类型转换成16进制字符串
#pragma mark - data转换为十六进制的string
+ (NSString *)hexStringFromData:(NSData *)myData{
Byte *bytes = (Byte *)[myData bytes];
//下面是Byte 转换为16进制。
NSString *hexStr=@"";
for(int i=0;i<[myData 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(@"hex = %@",hexStr);
return hexStr;
}
16进制字符串转换成NSData后按位异或
#pragma mark - 16进制字符串转换成data后按位异或
+ (NSString*)xorWithHexString:(NSString*)hexString range:(NSRange)range {
if (!hexString || (range.length == 0) ) {
return nil;
}
NSData *hexData = [NSString stringToHexData:[hexString substringWithRange:range]];
NSLog(@"hexData = %@",hexData);
Byte *byte = (Byte*)hexData.bytes;
for (int i=0; i<hexData.length - 1; i++) {
byte[0] ^= byte[i + 1];
}
NSLog(@"byte[0] = %02x",byte[0]);
NSData *resultData = [NSData dataWithBytes:byte length:1];
return [NSString hexStringFromData:resultData];
}
例如:10 01 00 04 31 32 33 34, 最后按位异或的结果是:11
10进制字符串转换成16进制字符串
#pragma mark - 10进制转16进制
+ (NSString *)hexStringFromDecString:(NSString*)decString
{
NSString *nLetterValue;
NSString *str =@"";
long long int ttmpig;
long long int tmpid = [decString longLongValue];
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;
}
}
NSLog(@"str = %@",str);
return str;
}
16进制字符串转换成2进制字符串
#pragma mark - 16进制字符串转2进制字符串
+ (NSString*)binStringFromHexString:(NSString*)hexStr {
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
for (int i=0;i<hexStr.length;i++) {
NSString* str = [hexStr substringWithRange:NSMakeRange(i, 1)];
[arr addObject:[self binString:str]];;
}
NSString *binStr = [arr componentsJoinedByString:@""];
NSLog(@"binStr = %@",binStr);
return binStr;
}
+ (NSString*)binString:(NSString*)str {
if ([str isEqualToString:@"0"]) {
return @"0000";
} else if ([str isEqualToString:@"1"]) {
return @"0001";
} else if ([str isEqualToString:@"2"]) {
return @"0010";
} else if ([str isEqualToString:@"3"]) {
return @"0011";
} else if ([str isEqualToString:@"4"]) {
return @"0100";
} else if ([str isEqualToString:@"5"]) {
return @"0101";
} else if ([str isEqualToString:@"6"]) {
return @"0110";
} else if ([str isEqualToString:@"7"]) {
return @"0111";
} else if ([str isEqualToString:@"8"]) {
return @"1000";
} else if ([str isEqualToString:@"9"]) {
return @"1001";
} else if ([str isEqualToString:@"a"]) {
return @"1010";
} else if ([str isEqualToString:@"b"]) {
return @"1011";
} else if ([str isEqualToString:@"c"]) {
return @"1100";
} else if ([str isEqualToString:@"d"]) {
return @"1101";
} else if ([str isEqualToString:@"e"]) {
return @"1110";
} else if ([str isEqualToString:@"f"]) {
return @"1111";
} else {
return nil;
}
}
这些有些是自己写的,有些是从网上找到的,亲测有效。