iOS socket发送Byte数组,进制转换,高低位,大小端,字节转换等(网关、蓝牙硬件传输常用)

使用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> 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> 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

你可能感兴趣的:(iOS socket发送Byte数组,进制转换,高低位,大小端,字节转换等(网关、蓝牙硬件传输常用))