• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
最近项目中要用到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<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; } 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<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; } 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<srcData.length; i++) { // [dstData appendData:[srcData subdataWithRange:NSMakeRange(srcData.length-1-i, 1)]]; // }//for 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; } ———————————————— 版权声明:本文为CSDN博主「X-Prince」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u011467458/article/details/52637355

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap