NSData 与 NSString,Byte数组,UIImage 的相互转换

  1. 1. NSData 与 NSString  
  2.   
  3. NSData-> NSString  
  4.   
  5. NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];  
  6.   
  7.    
  8.   
  9. NSString->NSData  
  10.   
  11. NSString *aString = @"1234abcd";  
  12.   
  13. NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];  
  14.   
  15.    
  16.   
  17. 2.NSData 与 Byte  
  18.   
  19. NSData-> Byte数组  
  20.   
  21. NSString *testString = @"1234567890";  
  22.   
  23. NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];  
  24.   
  25. Byte *testByte = (Byte *)[testData bytes];  
  26.   
  27. for(int i=0;i<[testData length];i++)  
  28.   
  29. printf("testByte = %d\n",testByte[i]);  
  30.   
  31.    
  32.   
  33. Byte数组-> NSData  
  34.   
  35. Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};  
  36.   
  37. NSData *adata = [[NSData alloc] initWithBytes:byte length:24];  
  38.   
  39.    
  40.   
  41. Byte数组->16进制数  
  42.   
  43. Byte *bytes = (Byte *)[aData bytes];  
  44.   
  45. NSString *hexStr=@"";  
  46.   
  47. for(int i=0;i<[encryData length];i++)  
  48.   
  49. {  
  50.   
  51. NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数  
  52.   
  53. if([newHexStr length]==1)  
  54.   
  55. hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];  
  56.   
  57. else   
  58.   
  59. hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];  
  60.   
  61. }  
  62.   
  63. NSLog(@"bytes 的16进制数为:%@",hexStr);  
  64.   
  65.    
  66.   
  67. 16进制数->Byte数组  
  68.   
  69. / 将16进制数据转化成Byte 数组  
  70.   
  71. NSString *hexString = @"3e435fab9c34891f"//16进制字符串  
  72.   
  73. int j=0;  
  74.   
  75. Byte bytes[128];  ///3ds key的Byte 数组, 128位  
  76.   
  77. for(int i=0;i<[hexString length];i++)  
  78.   
  79. {  
  80.   
  81. int int_ch;  /// 两位16进制数转化后的10进制数  
  82.   
  83.    
  84.   
  85. unichar hex_char1 = [hexString characterAtIndex:i]; 两位16进制数中的第一位(高位*16)  
  86.   
  87. int int_ch1;  
  88.   
  89. if(hex_char1 >= '0' && hex_char1 <='9')  
  90.   
  91. int_ch1 = (hex_char1-48)*16;    0 的Ascll - 48  
  92.   
  93. else if(hex_char1 >= 'A' && hex_char1 <='F')  
  94.   
  95. int_ch1 = (hex_char1-55)*16;  A 的Ascll - 65  
  96.   
  97. else   
  98.   
  99. int_ch1 = (hex_char1-87)*16;  a 的Ascll - 97  
  100.   
  101. i++;  
  102.   
  103.    
  104.   
  105. unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)  
  106.   
  107. int int_ch2;  
  108.   
  109. if(hex_char2 >= '0' && hex_char2 <='9')  
  110.   
  111. int_ch2 = (hex_char2-48);  0 的Ascll - 48  
  112.   
  113. else if(hex_char1 >= 'A' && hex_char1 <='F')  
  114.   
  115. int_ch2 = hex_char2-55;  A 的Ascll - 65  
  116.   
  117. else   
  118.   
  119. int_ch2 = hex_char2-87;  a 的Ascll - 97  
  120.   
  121.    
  122.   
  123. int_ch = int_ch1+int_ch2;  
  124.   
  125. NSLog(@"int_ch=%d",int_ch);  
  126.   
  127. bytes[j] = int_ch;  ///将转化后的数放入Byte数组里  
  128.   
  129. j++;  
  130.   
  131. }  
  132.   
  133. NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];  
  134.   
  135. NSLog(@"newData=%@",newData);  
  136.   
  137.    
  138.   
  139. 3. NSData 与 UIImage  
  140.   
  141. NSData->UIImage  
  142.   
  143. UIImage *aimage = [UIImage imageWithData: imageData];  
  144.   
  145.    
  146.   
  147. //例:从本地文件沙盒中取图片并转换为NSData  
  148.   
  149. NSString *path = [[NSBundle mainBundle] bundlePath];  
  150.   
  151. NSString *name = [NSString stringWithFormat:@"ceshi.png"];  
  152.   
  153. NSString *finalPath = [path stringByAppendingPathComponent:name];  
  154.   
  155. NSData *imageData = [NSData dataWithContentsOfFile: finalPath];  
  156.   
  157. UIImage *aimage = [UIImage imageWithData: imageData];  
  158.   
  159.    
  160.   
  161. UIImage-> NSData  
  162.   
  163. NSData *imageData = UIImagePNGRepresentation(aimae);   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!
在Objective-C中,可以通过NSDataNSString类提供的方法进行Byte数组和十六进制字符串的互转操作。 1. Byte数组转十六进制字符串: ``` // 定义一个Byte数组 Byte bytes[] = {0x12, 0x34, 0x56, 0x78}; // 将Byte数组转为NSData对象 NSData *data = [[NSData alloc] initWithBytes:bytes length:4]; // 使用NSData对象提供的方法将Byte数组转为十六进制字符串 NSString *hexString = [data description]; hexString = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""]; hexString = [hexString stringByReplacingOccurrencesOfString:@"<" withString:@""]; hexString = [hexString stringByReplacingOccurrencesOfString:@">" withString:@""]; NSLog(@"hexString = %@", hexString); // 输出结果:hexString = 12345678 ``` 2. 十六进制字符串转Byte数组: ``` // 定义一个十六进制字符串 NSString *hexString = @"12345678"; // 将十六进制字符串转为NSData对象 NSMutableData *data = [[NSMutableData alloc] init]; unsigned char whole_byte; char byte_chars[3] = {'\0','\0','\0'}; for (int i=0; i<[hexString length]/2; i++) { byte_chars[0] = [hexString characterAtIndex:i*2]; byte_chars[1] = [hexString characterAtIndex:i*2+1]; whole_byte = strtol(byte_chars, NULL, 16); [data appendBytes:&whole_byte length:1]; } // 将NSData对象转为Byte数组 Byte *bytes = (Byte *)[data bytes]; for (int i=0; i<[data length]; i++) { NSLog(@"bytes[%d] = %x", i, bytes[i]); // 输出结果:bytes[0] = 12、bytes[1] = 34、bytes[2] = 56、bytes[3] = 78 } ``` 需要注意的是,使用NSData的description方法将Byte数组转为十六进制字符串时,输出结果的格式为“<12 34 56 78>”,需要使用NSString的stringByReplacingOccurrencesOfString方法去掉空格和尖括号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值