Objective-C 中Socket常用转换机制(NSData,NSString,int,Uint8,Uint16,Uint32,byte[],int8_t,int16_t,int32_t,f...

  1. int类型转16进制hexstring
//int 转16进制
- (NSString *)hexFromInt:(NSInteger)val {
    return [NSString stringWithFormat:@"%X", val];
}

2.16进制转换为NSData

+ (NSData *)dataFromHexString:(NSString *)hexString
{
    NSAssert((hexString.length > 0) && (hexString.length % 2 == 0), @"hexString.length mod 2 != 0");
    NSMutableData *data = [[NSMutableData alloc] init];
    for (NSUInteger i=0; i

3.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> 4;
        Byte low = value & 0xf;
        [hexString appendFormat:@"%x%x", high, low];
    }//for
    return hexString;
}

4.NSString转int类型

int a = [aStrinng  intValue];

5.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];
}

6.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];
}

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

8.NSString 转NSData

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

9.NSData转Uint8

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

10.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;
}

11.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;
}

12.NSData转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);
    }

13.NSData的拼接

[data1 appendData:data2];

14.NSData转int (用CFSwapInt32BigToHost)

NSData *data4 = [completeData subdataWithRange:NSMakeRange(0, 4)];
int value = CFSwapInt32BigToHost(*(int*)([data4 bytes]));

以上可能会用到的反转字节序列代码

+ (NSData *)dataWithReverse:(NSData *)srcData
{
    //    NSMutableData *dstData = [[NSMutableData alloc] init];
    //    for (NSUInteger i=0; i

以上内容来自原址

15.NSData转int8_t

+(int8_t) int8FromBytes:(NSData *)data
{
    NSUInteger len = [data length];
    Byte *by=(Byte *)malloc(len);
    memcpy(by, [data bytes], len);
    int8_t ret=by[0] & 0xff;
    free(by);
    return ret;
    
}

16.NSData转int16_t

//bigEndian  传NO就行
+(int16_t) int16FromBytes:(NSData *)data bigEndian:(BOOL)bigEndian
{
    NSUInteger len = [data length];
    Byte *by=(Byte *)malloc(len);
    memcpy(by, [data bytes], len);
    int16_t ret=((by[1] & 0xFF) << 8) + (by[0] & 0xff);
    if (bigEndian) {
        ret=((by[0] & 0xFF) << 8) + (by[1] & 0xff);
    }
    free(by);
    return ret;
}

17.NSData转int32_t

//bigEndian  传NO就行
+(int32_t) int32FromBytes:(NSData *)data bigEndian:(BOOL)bigEndian
{
    NSUInteger len = [data length];
    Byte *by=(Byte *)malloc(len);
    memcpy(by, [data bytes], len);
    int32_t ret=(by[3] << 24) + ((by[2] & 0xFF) << 16) + ((by[1] & 0xFF) << 8) + (by[0] & 0xFF);
    if (bigEndian) {
        ret = ((by[0] & 0xFF) << 24) + ((by[1] & 0xFF) << 16) + ((by[2] & 0xFF) << 8) + (by[3] & 0xff);
    }
    free(by);
    return ret;
}

18.NSData转float

+(float)float32FromBytes:(NSData *)data bigEndian:(BOOL)bigEndian
{
    NSUInteger len = [data length];
    Byte *by=(Byte *)malloc(len);
    memcpy(by, [data bytes], len);
    float valf=0.0;
    char *temp=(char *)(&valf);
    if(bigEndian)
    {
        temp[3]=by[0];
        temp[2]=by[1];
        temp[1]=by[2];
        temp[0]=by[3];
    }else{
        temp[3]=by[3];
        temp[2]=by[2];
        temp[1]=by[1];
        temp[0]=by[0];
    }
    free(by);
    return valf;
}

19.int8_t转NSData(占两位)

+(NSData *) byteFromInt8:(int)val
{
    char myByteArray[] = {0};
    myByteArray[0]=val & 0xff;
    NSData *ret=[NSData dataWithBytes:myByteArray length:1];
    return ret;
}

20.int16_t 转NSData(占四位)

//bigEndian 传no
+(NSData *) bytesFromInt16:(int)val bigEndian:(BOOL)bigEndian
{
    char myByteArray[] = {0,0};
    myByteArray[0]=val & 0xff;
    myByteArray[1]=(val>>8) & 0xff;
    
    if(bigEndian)
    {
        myByteArray[1]=val & 0xff;
        myByteArray[0]=(val>>8) & 0xff;
    }else{
        myByteArray[0]=val & 0xff;
        myByteArray[1]=(val>>8) & 0xff;
    }
    
    NSData *ret=[NSData dataWithBytes:myByteArray length:2];
    return ret;
}

21.int32_t 转NSData(占八位)

//bigEndian 传no
+(NSData *) bytesFromInt32:(int)val bigEndian:(BOOL)bigEndian
{
    char myByteArray[] = {0,0,0,0};
    
    if(bigEndian)
    {
        myByteArray[3]=val & 0xff;
        myByteArray[2]=(val>>8) & 0xff;
        myByteArray[1]=(val>>16) & 0xff;
        myByteArray[0]=(val>>24) & 0xff;
    }else{
        myByteArray[0]=val & 0xff;
        myByteArray[1]=(val>>8) & 0xff;
        myByteArray[2]=(val>>16) & 0xff;
        myByteArray[3]=(val>>24) & 0xff;
    }
    
    
    NSData *ret=[NSData dataWithBytes:myByteArray length:4];
    return ret;
}

22.float转NSData

//bigEndian 传no
+(NSData *) bytesFromfloat32:(float)val bigEndian:(BOOL)bigEndian
{
    float valf=val;
    char myByteArray[] = {0,0,0,0};
    char *temp=(char *)(&valf);
    if(bigEndian)
    {
        myByteArray[3]=temp[0];
        myByteArray[2]=temp[1];
        myByteArray[1]=temp[2];
        myByteArray[0]=temp[3];
    }else{
        myByteArray[3]=temp[3];
        myByteArray[2]=temp[2];
        myByteArray[1]=temp[1];
        myByteArray[0]=temp[0];
    }
    NSData *ret=[NSData dataWithBytes:myByteArray length:4];
    return ret;
}

你可能感兴趣的:(Objective-C 中Socket常用转换机制(NSData,NSString,int,Uint8,Uint16,Uint32,byte[],int8_t,int16_t,int32_t,f...)