NSString、NSdata、Byte数组、UIImage 的相互转换


  1. NSData-> NSString类型

  2. NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
  1. NSString->NSData类型
  2. NSString *aString = @"1234abcd";
  3. NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];

2.NSData 与 Byte
NSData-> Byte数组
  1. NSString *testString = @"1234567890";
  2. NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
  3. Byte *testByte = (Byte *)[testData bytes];
  4. for(int i=0;i<[testData length];i++)
  5. printf("testByte = %d\n",testByte[i]);
Byte数组-> NSData
  1. 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};
  2. NSData *adata = [[NSData alloc] initWithBytes:byte length:24];
Byte数组->16进制数
  1. Byte *bytes = (Byte *)[aData bytes];
  2. NSString *hexStr=@"";
  3. for(int i=0;i<[encryData length];i++)
  4. {
  5. NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff]; ///16进制数
  6. if([newHexStr length]==1)
  7. hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
  8. else
  9. hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
  10. }
  11. NSLog(@"bytes 的16进制数为:%@",hexStr);

16进制数->Byte数组
  1. NSString *hexString = @"3e435fab9c34891f"; //16进制字符串
  2. int j=0;
  3. Byte bytes[128]; 
  4. ///3ds key的Byte 数组, 128位
  5. for(int i=0;i<[hexString length];i++)
  6. {
  7. int int_ch;  // 两位16进制数转化后的10进制数

  8. unichar hex_char1 = [hexString characterAtIndex:i];     //两位16进制数中的第一位(高位*16)
  9. int int_ch1;
  10. if(hex_char1 >= '0' && hex_char1 <='9')
  11. int_ch1 = (hex_char1-48)*16;    0 的Ascll - 48
  12. else if(hex_char1 >= 'A' && hex_char1 <='F')
  13. int_ch1 = (hex_char1-55)*16; A 的Ascll - 65
  14. else
  15. int_ch1 = (hex_char1-87)*16; a 的Ascll - 97
  16. i++;

  17. unichar hex_char2 = [hexString characterAtIndex:i]; //两位16进制数中的第二位(低位)
  18. int int_ch2;
  19. if(hex_char2 >= '0' && hex_char2 <='9')
  20. int_ch2 = (hex_char2-48);                           // 0 的Ascll - 48
  21. else if(hex_char1 >= 'A' && hex_char1 <='F')
  22. int_ch2 = hex_char2-55;                             // A 的Ascll - 65
  23. else
  24. int_ch2 = hex_char2-87;                             // a 的Ascll - 97

  25. int_ch = int_ch1+int_ch2;
  26. NSLog(@"int_ch=%d",int_ch);
  27. bytes[j] = int_ch;                                  //将转化后的数放入Byte数组里
  28. j++;
  29. }
  30. NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];
  31. NSLog(@"newData=%@",newData);

3. NSData 与 UIImage
  1. NSData->UIImage
  2. UIImage *aimage = [UIImage imageWithData: imageData];

  3. //例:从本地文件沙盒中取图片并转换为NSData
  4. NSString *path = [[NSBundle mainBundle] bundlePath];
  5. NSString *name = [NSString stringWithFormat:@"ceshi.png"];
  6. NSString *finalPath = [path stringByAppendingPathComponent:name];
  7. NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
  8. UIImage *aimage = [UIImage imageWithData: imageData];

  9. UIImage-> NSData
  10. NSData *imageData = UIImagePNGRepresentation(aimae);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值