iOS 记录蓝牙传输相关进制转换/字节转换/NSData转换/大小端转换

项目中有较多的多种蓝牙外设的对接 遇到的数据传输比较多 打算从使用框架、模块到传输协议一个系列记录一下
先记录一些数据传输时常用到的转换方式吧 后面再补充

1、整形转换成两个字节
int value = 100;
unsigned char  byte1 = (value & 0xff00)>>8; //高8位
unsigned char  byte2 = (value & 0xff);//低8位
NSLog(@"byte1= %x   byte2= %x ",byte1,byte2);
//byte1 = 0;  byte2= 64;

2、两个字节转换成十进制整数

char bytes[]={byte1,byte2};

unsigned char  by1 = (bytes[0] & 0xff); //高8位
unsigned char  by2 = (bytes[1] & 0xff);//低8位

int   temp  = (by2|(by1<<8));

NSLog(@"temp = %d",temp);
  

3、整形转换成三个字节


int value =134456;

unsigned char  byte1 = (value & 0xff0000)>>16;//最高8位
unsigned char  byte2 = (value & 0xff00)>>8;//中间8位
unsigned char  byte3 = (value & 0xff);//低8位

NSLog(@"byte1= %x   byte2= %x byte3= %x ",byte1,byte2,byte3);

    //byte1= 2   byte2= d byte3= 38

4、三个字节转换成十进制整数


char bytes[]={byte1,byte2,byte3};

unsigned char  by1 = (bytes[0] & 0xff); //高8位
unsigned char  by2 = (bytes[1] & 0xff);//中8位
unsigned char  by3 = (bytes[2] & 0xff);//低8位

int   temp  = (by3|(by2<<8)|(by1<<16));

NSLog(@"temp = %d",temp);

 //temp = 134456

——————有更多的需求以此类推——————

5、16进制字符串转10进制number

// 16进制字符串转10进制
+ (NSNumber *) numberHexString:(NSString *)aHexString
{
    if (nil == aHexString){
        return nil;
    }
    NSScanner * scanner = [NSScanner scannerWithString:aHexString];
    unsigned long long longlongValue;
    [scanner scanHexLongLong:&longlongValue];

    //将整数转换为NSNumber,存储到数组中,并返回.
    NSNumber * hexNumber = [NSNumber numberWithLongLong:longlongValue];
    
    return hexNumber;
}

6、16进制字符串转NSData

//将16进制的字符串转换成NSData
+ (NSMutableData *)convertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] %2 == 0) {
        range = NSMakeRange(0,2);
    } else {
        range = NSMakeRange(0,1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    
    return hexData;
}

7、简单的10进制转16进制字符串

//int 转16进制
+ (NSString *)hexFromInt:(NSInteger)val {
    return [NSString stringWithFormat:@"%X", val];
}

8、NSData转器16进制

+ (NSString *)hexStringFromData:(NSData *)data
{
    NSAssert(data.length > 0, @"data.length <= 0");
    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;
}

9、NSString 转NSData

NSData *data = [aString  dataUsingEncoding:NSUTF8StringEncoding];// 通过utf-8转为data

10、uint8、NSData(占两位)互转

//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];
}

//NSData转uint8_t
+ (uint8_t)uint8FromBytes:(NSData *)fData
{
    NSAssert(fData.length == 1, @"uint8FromBytes: (data length != 1)");
    NSData *data = fData;
    uint8_t val = 0;
    [data getBytes:&val length:1];
    return val;
}

11、uint16 、NSData(占四位)互转

//uint16 转NSData
+ (NSData *)bytesFromUInt16:(uint16_t)val
{
    NSMutableData *valData = [[NSMutableData alloc] init];
    
    unsigned char valChar[2];
    valChar[0] = 0xff & val;
    valChar[1] = (0xff00 & val) >> 8;
    [valData appendBytes:valChar length:2];
    
    return [self dataWithReverse:valData];
}

//NSData转uint16
+ (uint16_t)uint16FromBytes:(NSData *)fData
{
    NSAssert(fData.length == 2, @"uint16FromBytes: (data length != 2)");
    NSData *data = [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;
}

12、uint32 与NSData互转

//uint32 转NSData(占八位)
+ (NSData *)bytesFromUInt32:(uint32_t)val
{
    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];
    
    return [self dataWithReverse:valData];
}

//NSData转Uint32
+ (uint32_t)uint32FromBytes:(NSData *)fData
{
    NSAssert(fData.length == 4, @"uint32FromBytes: (data length != 4)");
    NSData *data = [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;
}

13、data转byte

uint8_t byteArray[[data length]];
[data getBytes:&byteArray length:[data length]];

for (int i = 0; i < [data length] ; i++ ) {
     Byte byte = byteArray[i];
     NSLog(@"--byte%x",byte);    
}

14、NSData转int (用CFSwapInt32BigToHost)

NSData *data4 = [completeData subdataWithRange:NSMakeRange(0, 4)];
int value = CFSwapInt32BigToHost(*(int*)([data4 bytes]));
//NSData转成NSinteger
- (NSInteger)intFormData:(NSData *)data
{
    const uint8_t *reportData = [data bytes];
    uint16_t bpm = 0;
    
    if ((reportData[0] & 0x01) == 0){
        /* uint8 bpm */
        bpm = reportData[1];
    }
    else{
        /* uint16 bpm */
        bpm = CFSwapInt16LittleToHost(*(uint16_t *)(&reportData[1]));
    }
    return bpm;
}

15、可能会用到的反转字节序列代码

+ (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];
    }
    
    return dstData;
}

16、大小端转换

#define Tranverse16(X)  ((((UInt16)(X) & 0xff00) >> 8) |(((UInt16)(X) & 0x00ff) << 8))

#define Tranverse32(X)  ((((UInt32)(X) & 0xff000000) >> 24) | (((UInt32)(X) & 0x00ff0000) >> 8) | (((UInt32)(X) & 0x0000ff00) << 8) | (((UInt32)(X) & 0x000000ff) << 24))

#define Tranverse64(X)  ((((UInt64)(X) & 0xff00000000000000) >> 56) | (((UInt64)(X) & 0x00ff000000000000) >> 40) | (((UInt64)(X) & 0x0000ff0000000000) >> 24) | (((UInt64)(X) & 0x000000ff00000000) >> 8) | (((UInt64)(X) & 0x00000000ff000000) << 8) | (((UInt64)(X) & 0x0000000000ff0000) << 24) | (((UInt64)(X) & 0x000000000000ff00) << 40) | (((UInt64)(X) & 0x00000000000000ff) << 56))


作者:iii余光
链接:https://www.jianshu.com/p/0fadbb15cf7f
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2021-07-06 10:15  brave-sailor  阅读(715)  评论(0编辑  收藏  举报