1.NSDictionary类型转换为NSData类型:

  1. NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
  2. @"balance", @"key",
  3. @"remaining balance", @"label",
  4. @"45", @"value",
  5. @"USD", @"currencyCode",nil];
  6. NSMutableData *data = [[NSMutableData alloc] init];
  7. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  8. [archiver encodeObject:params forKey:@"Some Key Value"];
  9. [archiver finishEncoding];

2.NSData类型转换为NSDictionary类型:

  1. //NSData -> NSDictionary
  2. NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
  3. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  4. NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
  5. [unarchiver finishDecoding];
  6. [unarchiver release];
  7. [data release];

3.NSDictionary类型转换为JSON数据类型:

  1. //NSDictionary -> JSON:
  2. NSString *jsonStr=[dict JSONRepresentation];

注意JSON与NSDictionary类型的转换一般会用到第三方公开的JSON库,读者可以在网上找到很多。

1. NSData 与 NSString
NSData-> NSString
NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];

NSString->NSData
NSString *aString = @"1234abcd";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];

2.NSData 与 Byte
NSData-> Byte数组
NSString *testString = @"1234567890";
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte *testByte = (Byte *)[testData bytes];
for(int i=0;i<[testData length];i++)
printf("testByte = %d\n",testByte[i]);

Byte数组-> NSData
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};
NSData *adata = [[NSData alloc] initWithBytes:byte length:24];

Byte数组->16进制数
Byte *bytes = (Byte *)[aData bytes];
NSString *hexStr=@"";
for(int i=0;i<[encryData length];i++)
{
NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff]; ///16进制数
if([newHexStr length]==1)
hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
else
hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
NSLog(@"bytes 的16进制数为:%@",hexStr);

16进制数->Byte数组
///// 将16进制数据转化成Byte 数组
NSString *hexString = @"3e435fab9c34891f"; //16进制字符串
int j=0;
Byte bytes[128];

///3ds key的Byte 数组, 128位

for(int i=0;i<[hexString length];i++)

{

int int_ch;  /// 两位16进制数转化后的10进制数

unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
int int_ch1;
if(hex_char1 >= '0' && hex_char1 <='9')
int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
else if(hex_char1 >= 'A' && hex_char1 <='F')
int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
else
int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
i++;
 
unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
int int_ch2;
if(hex_char2 >= '0' && hex_char2 <='9')
int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
else if(hex_char1 >= 'A' && hex_char1 <='F')
int_ch2 = hex_char2-55; //// A 的Ascll - 65
else
int_ch2 = hex_char2-87; //// a 的Ascll - 97

int_ch = int_ch1+int_ch2;

NSLog(@"int_ch=%d",int_ch);

bytes[j] = int_ch;  ///将转化后的数放入Byte数组里

j++;

}

NSData *newData = [[NSData alloc] initWithBytes:bytes length:128];

NSLog(@"newData=%@",newData);

3. NSData 与 UIImage

NSData->UIImage

UIImage *aimage = [UIImage imageWithData: imageData];

//例:从本地文件沙盒中取图片并转换为NSData

NSString *path = [[NSBundle mainBundle] bundlePath];

NSString *name = [NSString stringWithFormat:@"ceshi.png"];

NSString *finalPath = [path stringByAppendingPathComponent:name];

NSData *imageData = [NSData dataWithContentsOfFile: finalPath];

UIImage *aimage = [UIImage imageWithData: imageData];

UIImage-> NSData

NSData *imageData = UIImagePNGRepresentation(aimae);

说法2:

前言:今天发现一些同学 在NSDictionary转NSData竟然抓瞎了,所以就粘出来这个简单的方法!

大家在做网络请求的时间,如NSConnection 把NSData转NSDictionary的事了吧!  这里是一样的Foundation库提供的NSJSONSerialization 的一个方法: dataWithJSONObject:options:error:
如题代码如下:

NSDictionary *dic = @{@"name":@"macRong",@"webHome":@"eqi.cc"};

NSData *data =    [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];

NSLog(@"NSDictionary  NSData = %@",data);

NSLog(@"NSData  NSDictionary =%@",dictionary);

iOS NSDictionary、NSData、JSON等 数据类型相互转换的更多相关文章

  1. iOS NSDictionary 转Json 去掉换行去掉空格

    //dic 转json 如果用系统自带的会出现空格. + (NSString *)returnJSONStringWithDictionary:(NSDictionary *)dictionary{ ...

  2. iOS NSDictionary <--> NSString(JSON) in Objc

    NSDictionary --> NSString + (NSString*)stringINJSONFormatForObject:(id)obj { NSData *jsonData = [ ...

  3. iOS NSDictionary、NSData、JSON数据类型相互转换

    iOS经常需要用到数据类型的转换,下面列举一下常用类型的转换. 1.NSDictionary类型转换为NSData类型: //NSDictionary -> NSData: NSDictiona ...

  4. ios中常用数据类型相互转换

    ios中常用数据类型相互转换 //1. NSMutableArray和NSArray互转 // NSArray转为NSMutableArray NSMutableArray *arrM = [arr ...

  5. iOS 字典与JSON相互转换

    iOS 字典与JSON相互转换 首先简单说一下为什么会写这种幼稚的文章. 现在的网络请求几乎都是AFN完成的,AFN也为我们写了了JSON转换字典的方法,但是不要忘记后台是一个很爱用JSON的人群,H ...

  6. iOS 将NSArray、NSDictionary转换为JSON格式进行网络传输

    http://blog.csdn.net/worldzhy/article/details/49982491 将NSArray.NSDictionary转换为JSON格式进行网络传输,是经常用到的,但 ...

  7. iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转

    iOS开发--字典(NSDictionary)和JSON字符串(NSString)之间互转 1. 字典转Json字符串 // 字典转json字符串方法 -(NSString *)convertToJs ...

  8. iOS开发之JSON格式数据的生成与解析

    本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...

  9. 转载 -- iOS开发之JSON格式数据的生成与解析

    本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...

随机推荐

  1. 2013=11=12 SQL 实验

    --22. 查询选修课程成绩至少有一门在80分以上的学生学号: select distinct sno from sc where grade>80 go --23. 查询选修课程成绩均在80分 ...

  2. Java学习日记-6 继承

    继承1.基本介绍 面向对象程序设计三大原则之一.被继承的称为父类,继承类称为子类.关键字:extends例子: class TwoDshape{ double width; double height ...

  3. [转载]监控 Linux 性能的 18 个命令行工具

    转自:http://www.kuqin.com/shuoit/20140219/338066.html 对于系统和网络管理员来说每天监控和调试Linux系统的性能问题是一项繁重的工作.在IT领域作为一 ...

  4. 297 - Quadtrees (UVa)

    Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behind t ...

  5. Java程序员的日常—— 垃圾回收中引用类型的作用

    在Java里面,是不需要太过于关乎垃圾回收,但是这并不意味着开发者可以不了解垃圾回收的机制,况且在java中内存泄露也是家常便饭的事情.因此了解垃圾回收的相关知识就显得很重要了. 引用,在垃圾回收中是 ...

  6. 自然数e这家伙怎么蹦跶出来的?

    自然数e这家伙怎么蹦跶出来的? 之前看过一篇中文介绍自然数e的blog,引起了我的兴趣 原文是阮一峰大牛(我认为必须很有必要尊敬的称,大牛)嚼烂了吐出来的哈哈,只是我认为还是自己去看原文比較好 感觉非 ...

  7. 苹果Swift编程语言新手教程【中国版】

    Swift代码语言教程:在刚刚过去的WWDC2014大会上,苹果公司新公布了一种编程语言Swift.据悉.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.对于广 ...

  8. STL之deque双向队列

    deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,提供随机访问,deque在接口上和vector非常相似,下面列出deque的常用成员函数: Table 6.9. C ...

  9. Java基础知识强化22:Java中数据类型转换

    数据类型转换: (1). 自动转换 低级变量可以直接转换为高级变量,这叫自动类型转换.比如: byte b: int b:  long b:  float b:   double  b: 上面的语句可 ...

  10. 95秀-异步http请求完整过程

    最终调用时的代码     private void ansyClearApplyInfor() {         RequestParams params = new RequestParams() ...