Nsdata,nsimage,nsdictionary,nsstring,nsinteger,float,nsurl and so on convert each other

Source: Internet
Author: User

Nsdata,nsimage,nsdictionary,nsstring,nsinteger,float,nsurl and so on convert each other

One: NSString and Nsurl Conversion

[CPP]View Plaincopyprint?
    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];

Two: NSString and NSData conversions (JSON strings can be converted to each other)

[CPP]View Plaincopyprint?
    1. Nsdata-> NSString
    2. NSString *teststring = [[NSString alloc] Initwithdata:testdata encoding:nsutf8stringencoding];
    3. Nsstring->nsdata
    4. NSString *astring = @ "I am nsstring";
    5. NSData *testdata = [teststring datausingencoding:nsutf8stringencoding];

Three: NSData and Nsimage conversion

[CPP]View Plaincopyprint?< param name= "wmode" value= "Transparent" >
    1. Nsdata->uiimage
    2. UIImage *testimage = [UIImage imagewithdata:imagedata];
    3. Uiimage->nsdata
    4. NSData *imagedata = uiimagepngrepresentation (testimage)
    5. Or
    6. NSData *imagedata = uiimagejpegrepresentation (testimage,1.0)

IV: NSData and Nsdictionary conversion

[CPP]View Plaincopyprint?
  1. Nsdata-->nsdictionary
  2. Nsdictionary *testdict = [Nsjsonserialization jsonobjectwithdata:testdata options:nsjsonreadingmutablecontainers Error:nil]
  3. Or
  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];

V: nsdictionary and JSON conversion

[CPP]View Plaincopyprint?
    1. Nsdictionary-JSON://This method is in the Sbjson class library, you need to import SBJson.h
    2. NSString *jsonstr=[dict Jsonrepresentation];
    3. Recommendations (especially HTTP retrieval of the JSONSTR conversions below)
    4. Json->nsdictionary:
    5. NSData *jsondata = [Testjsonstr datausingencoding:nsutf8stringencoding];
    6. Nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers Error: Nil

Excerpt from another student's

NSData with byte
nsdata-> byte array
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 array, 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 array->16 binary
byte *bytes = (BYTE *) [aData bytes];
NSString *[email protected] "";
for (int i=0;i<[encrydata length];i++)
{
NSString *newhexstr = [NSString stringwithformat:@ "%x", Bytes[i] &0xff]; 16 Binary number
if ([Newhexstr length]==1)
Hexstr = [NSString stringwithformat:@ "%@0%@", hexstr,newhexstr];
else
Hexstr = [NSString stringwithformat:@ "%@%@", Hexstr,newhexstr];
}
NSLog (@ "bytes 16 binary number is:%@", hexstr);

Nsdata,nsimage,nsdictionary,nsstring,nsinteger,float,nsurl and so on convert each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.