码迷,mamicode.com
首页 > Web开发 > 详细

NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换

时间:2014-12-27 11:23:17      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换  

 

一:NSString和NSURL 转换

 

  1. //NSString->NSURL  
  2. NSString *urlString=[@"http://www.google.com" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
  3. NSURL *url=[NSURL URLWithString:urlString];   
  4. //NSURL->NSString   
  5. NSString *urlString=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  

 

二:NSString和NSData转换(即可作json串互相转换)

 

  1. //NSData-> NSString  
  2. NSString *testString = [[NSString alloc] initWithData:testData encoding:NSUTF8StringEncoding];  
  3. //NSString->NSData  
  4. NSString *aString = @"我是NSString";  
  5. NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];  

 

三:NSData和NSImage转换

 

  1. //NSData->UIImage  
  2. UIImage *testImage = [UIImage imageWithData: imageData];  
  3. //UIImage->NSData  
  4. NSData *imageData = UIImagePNGRepresentation(testImage)  
  5. //或者  
  6. NSData *imageData = UIImageJPEGRepresentation(testImage,1.0)  

 

四:NSData和NSDictionary转换

 

 

  1. //NSData-->NSDictionary  
  2. NSDictionary *testDict = [NSJSONSerialization JSONObjectWithData:testData options:NSJSONReadingMutableContainers error:nil]  
  3. //或者  
  4. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:testData];    
  5. NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];    
  6. [unarchiver finishDecoding];  
  7. //NSDictionay --> NSData  
  8. NSMutableData *data = [[NSMutableData alloc] init];    
  9. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];    
  10. [archiver encodeObject:params forKey:@"Some Key Value"];    
  11. [archiver finishEncoding];  

 

五:NSDictionary和JSON转换

 

  1. //NSDictionary -> JSON:  //这个方法是SBJson类库里的,需要导入SBJson.h  
  2. NSString *jsonStr=[dict JSONRepresentation];  
  3. //建议(特别是http取回来的jsonStr转换如下)  
  4. //JSON->NSDictionary:  
  5. NSData *jsonData = [testJsonStr dataUsingEncoding:NSUTF8StringEncoding];  
  6. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]  

 

//摘录其他同学的

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);

NSData,NSImage,NSDictionary,NSString,NSInteger,Float,NSURL 等等互相转换

标签:

原文地址:http://www.cnblogs.com/angongIT/p/4188123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!