使用socket发送数据的时候,有时要求发送Byte数组,在网关(Lora)、蓝牙外设等硬件传输的时候常常用到。
安卓端处理起来较容易,iOS因为语言的问题,处理起来没有那么友好。
我项目中用到的是与Lora网关传输的数据转换代码,基本上所有的数据转换情况都包含了。
@implementation NSString (Hex)
///NSData转16进制
+(NSString *)hexStringFromData:(NSData *)data{
if (data.length<=0) {
return @"";
}
NSMutableString *hexString = [[NSMutableString alloc] init];
const Byte *bytes = data.bytes;
for (NSUInteger i=0; i<data.length; i++) {
Byte value = bytes[i];
Byte high = (value & 0xf0) >> 4;
Byte low = value & 0xf;
[hexString appendFormat:@"%x%x", high, low];
}//for
return hexString;
}
/// 16进制转NSData
+(NSData *)dataFromHexString:(NSString *)hexString{
if (hexString.length == 0 ||
hexString.length % 2) {
return [NSData data];
}
NSMutableData *data = [[NSMutableData alloc] init];
for (NSUInteger i=0; i<hexString.length; i+=2) {
NSRange tempRange = NSMakeRange(i, 2);
NSString *tempStr = [hexString substringWithRange:tempRange];
NSScanner *scanner = [NSScanner scannerWithString:tempStr];
unsigned int tempIntValue;
[scanner scanHexInt:&tempIntValue];
[data appendBytes:&tempIntValue length:1];
}
return data;
}
//int 转16进制
+ (NSString *)hexFromInt:(NSInteger)val {
return [NSString stringWithFormat:@"%X", val];
}
/// NSString 转NSData
+(NSData*)dataFromString:(NSString *)aString{
// 通过utf-8转为data
NSData* data = [aString dataUsingEncoding:NSUTF8StringEncoding];
return data;
}
/// uint8转NSData(占两位)
+(NSData *)byteFromUInt8:(uint8_t)val{
NSMutableData *valData = [[NSMutableData alloc] init];
unsigned char valChar[1];
valChar[0] = 0xff & val;
[valData appendBytes:valChar length:1];
return [self dataWithReverse:valData];
}
///uint16转NSData(占四位)
///prioryHight: YES(转化的Data为高位在前),NO(转化的Data为低位在前)
+(NSData *)byteFromUInt16:(uint16_t)val prioryHight:(BOOL)prioryHight{
NSMutableData *valData = [[NSMutableData alloc] init];
unsigned char valChar[2];
valChar[0] = 0xff & val;
valChar[1] = (0xff00 & val) >> 8;
[valData appendBytes:valChar length:2];
NSData* resultData = valData;
if (prioryHight) {
resultData = [self dataWithReverse:valData];
}
return resultData;
}
/// uint32转NSData(占八位)
/// prioryHight: YES(转化的Data为高位在前),NO(转化的Data为低位在前)
+(NSData *)byteFromUInt32:(uint32_t)val prioryHight:(BOOL)prioryHight{
NSMutableData *valData = [[NSMutableData alloc] init];
unsigned char valChar[4];
valChar[0] = 0xff & val;
valChar[1] = (0xff00 & val) >> 8;
valChar[2] = (0xff0000 & val) >> 16;
valChar[3] = (0xff000000 & val) >> 24;
[valData appendBytes:valChar length:4];
NSData* resultData = valData;
if (prioryHight) {
resultData = [self dataWithReverse:valData];
}
return resultData;
}
/// NSData转Uint8
+(uint8_t)uint8FromBytes:(NSData *)fData{
if (fData.length!=1) {
NSLog(@"uint8FromBytes: (data length != 1)");
return 0;
}
NSData *data = fData;
uint8_t val = 0;
[data getBytes:&val length:1];
return val;
}
/// NSData转Uint16
+(uint16_t)uint16FromBytes:(NSData *)fData prioryHight:(BOOL)prioryHight{
if (fData.length!=2) {
NSLog(@"uint16FromBytes: (data length != 2)");
return 0;
}
NSData *data = fData;
if (prioryHight) {
[self dataWithReverse:fData];
}
uint16_t val0 = 0;
uint16_t val1 = 0;
[data getBytes:&val0 range:NSMakeRange(0, 1)];
[data getBytes:&val1 range:NSMakeRange(1, 1)];
uint16_t dstVal = (val0 & 0xff) + ((val1 << 8) & 0xff00);
return dstVal;
}
/// NSData转Uint32
+(uint32_t)uint32FromBytes:(NSData *)fData prioryHight:(BOOL)prioryHight{
if (fData.length!=4) {
NSLog(@"uint32FromBytes: (data length != 4)");
return 0;
}
NSData *data = fData;
if (prioryHight) {
[self dataWithReverse:fData];
}
uint32_t val0 = 0;
uint32_t val1 = 0;
uint32_t val2 = 0;
uint32_t val3 = 0;
[data getBytes:&val0 range:NSMakeRange(0, 1)];
[data getBytes:&val1 range:NSMakeRange(1, 1)];
[data getBytes:&val2 range:NSMakeRange(2, 1)];
[data getBytes:&val3 range:NSMakeRange(3, 1)];
uint32_t dstVal = (val0 & 0xff) + ((val1 << 8) & 0xff00) + ((val2 << 16) & 0xff0000) + ((val3 << 24) & 0xff000000);
return dstVal;
}
///NSData转int
+(int)intFromData:(NSData*)fData{
if (fData.length == 4) {
NSLog(@"uint32FromBytes: (data length != 4)");
return 0;
}
int result = CFSwapInt32BigToHost(*(int*)([fData bytes]));
return result;
}
/// 反转字节序列代码
+(NSData *)dataWithReverse:(NSData *)srcData{
NSUInteger byteCount = srcData.length;
NSMutableData *dstData = [[NSMutableData alloc] initWithData:srcData];
NSUInteger halfLength = byteCount / 2;
for (NSUInteger i=0; i<halfLength; i++) {
NSRange begin = NSMakeRange(i, 1);
NSRange end = NSMakeRange(byteCount - i - 1, 1);
NSData *beginData = [srcData subdataWithRange:begin];
NSData *endData = [srcData subdataWithRange:end];
[dstData replaceBytesInRange:begin withBytes:endData.bytes];
[dstData replaceBytesInRange:end withBytes:beginData.bytes];
}//for
return dstData;
}
/// 检验和
+(NSString*)hexCheckSumFromString:(NSString *)aString{
if (aString.length==0) {
return @"";
}
int iTotal = 0;
int iLen = (int)aString.length;
int iNum = 0;
while (iNum < iLen) {
NSString* byteString = [aString substringWithRange:NSMakeRange(iNum, 2)];
iTotal += strtoul([byteString UTF8String], 0, 16);
iNum = iNum + 2;
}
//用256求余最大是255,即16进制的FF
int iMod = iTotal % 256;
//int转16进制串
NSString* sHex = [NSString stringWithFormat:@"%x",iMod];
//如果不够校验位的长度,补0,这里用的是两位校验
if (sHex.length<2) {
sHex = [NSString stringWithFormat:@"0%@",sHex];
}
return sHex;
}
/// 字符串转ascii字符串
+(NSString*)asciStringFromString:(NSString *)aString{
NSMutableString* muString = [[NSMutableString alloc] init];
const char *ch = [aString cStringUsingEncoding:NSASCIIStringEncoding];
for (int i=0; i<strlen(ch); i++) {
[muString appendString:[NSString stringWithFormat:@"%x",ch[i]]];
}
return muString;
}
@end