最近项目中要用到socket通讯,由于涉及到组包问题,所以需要数据类型之间的来回转换,现在分享出来
如果想要请教Socket的问题请留言,我会随时回答的
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
NSString *tempStr = [hexString substringWithRange:tempRange];
NSScanner *scanner = [NSScanner scannerWithString:tempStr];
unsigned int tempIntValue;
[scanner scanHexInt:&tempIntValue];
[data appendBytes:&tempIntValue length:1];
}
return data;
}
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
Byte high = (value & 0xf0) >> 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
// }//for
NSUInteger byteCount = srcData.length;
NSMutableData *dstData = [[NSMutableData alloc] initWithData:srcData];
NSUInteger halfLength = byteCount / 2;
for (NSUInteger i=0; i
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;
}
————————————————
版权声明:本文为CSDN博主「X-Prince」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011467458/article/details/52637355