Conversion between various data types in object-C

Source: Internet
Author: User

1. nsdata and nsstring
Nsdata --> nsstring
Nsstring * astring = [[nsstring alloc] initwithdata: aData encoding: nsutf8stringencoding];
Nsstring --> nsdata
Nsstring * astring = @ "1234 ";
Nsdata * aData = [astring datausingencoding: nsutf8stringencoding];

2. nsdata and byte
Nsdata --> byte
Nsstring * teststring = @ "1234567890 ";
Nsdata * testdata = [teststring datausingencoding: nsutf8stringencoding];
Byte * testbyte = (byte *) [testdata bytes];
Byte --> nsdata
Byte byte [] = {, 21 };
Nsdata * aData = [[nsdata alloc] initwithbytes: Byte Length: 24];

3. nsdata and uiimage
Nsdata --> uiimage
Uiimage * aimage = [uiimage imagewithdata: imagedata];
// Example: extract the image from the local file sandbox and convert it to 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 );

4. nsdata and nsmutabledata
Nsdata --> msmutabledata
Nsdata * Data = [[nsdata alloc] init];
Nsmutabledata * mdata = [nsmutabledata alloc] init];
Mdata = [nsdata datawithdata: Data];

 

5. Merge nsdata into an nsmutabledata

-(Nsstring *) filepathwithname :( nsstring *) filename {nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectory = [paths objectatindex: 0]; return [documentsdirectory stringbyappendingpathcomponent: Filename];}-(bool) Application :( uiapplication *) Application didfinishlaunchingwithoptions :( nsdictionary *) launchoptions {// audio file path nsstring * mp3path1 = [[nsbundle mainbundle] pathforresource: @ "1" oftype: @ "MP3"]; nsstring * mp3path2 = [[nsbundle mainbundle] pathforresource: @ "2" oftype: @ "MP3"]; // audio data nsdata * sound1data = [[nsdata alloc] initwithcontentsoffile: mp3path1]; nsdata * sound2data = [[nsdata alloc] Audio: mp3path2]; // merge audio nsmutabledata * Sounds = [nsmutabledata alloc]; [sounds appenddata: sound1data]; [sounds appenddata: sound2data]; // Save the audio nslog (@ "Data Length: % d", [sounds length]); [sounds writetofile: [self filepathwithname: @ "tmp1_"] atomically: Yes]; [Window makekeyandvisible]; return yes ;}

6 nsstring merge

Nsstring * string; // result string nsstring * string1, string2; // an existing string. You must connect string1 and string2. // method 1. string = [nsstring initwithformat: @ "% @, % @", string1, string2]; // method 2. string = [string1 stringbyappendingstring: string2]; // method 3. string = [String stringbyappendingformat: @ "% @, % @", string1, string2];

 

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.