Object-c data type conversion NSData turn Nsstring,byte,uiimage

Source: Internet
Author: User

NSString converted to a Unicode String: (nsstring*) fname = @ "Test"; char fnamestr[10];memcpy (FNAMESTR, [fname cstringusingencoding:nsunicodestringencoding], ([fname length]));
Compared to strcpy, memcpy is not going to end up with ' n ', but will definitely copy the nth bytes
nsstring Conversion to char * NSString * str= @ "Test"; const char * a =[STR utf8string];
3.char * converted to NSString
NSString *str=[nsstring stringwithcstring  encoding:nsutf8stringencoding];

char * Conversion NSData method One:   char * a = (char*) malloc (sizeof (byte) *16);   NSData *data = [NSData datawithbytes:a   Length:strlen (a)]; method Two:    convert to NSString:-(ID) initwithutf8string: (const char *) bytes    nsdata Conversion char *  nsdata data;  nsdata conversion nsstring;
nsdata* data;
nsstring* astr= [NSString alloc] Initwithdata:data encoding:nsasciistringencoding];
nsstring Transforming NSData Objects nsdata* xmlData = [@ "testdata" datausingencoding:nsutf8stringencoding];

NSString Conversion Nsurl  
Nsurl *url = [Nsurl urlwithstring:[str   stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
NSString *urlstring=[@ "http://www.google.com/search?client=safari&rls=en&q= Search &ie=utf-8&oe=utf-8 [Stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
Nsurl Conversion NSString
Nsurl *url=[nsurl urlwithstring:urlstring];
NSString *s=[[url absolutestring] stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];

Nsarray *arr = [urlstr componentsseparatedbystring:@ "&"]; Nsurl *url = [Nsurl Urlwithstring:[[arr objectatindex:0] stringbyaddingpercentescapesusingencoding: Nsutf8stringencoding]]; Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; [Request sethttpmethod:@ "POST"];

Nsmutablearray *_array = [[Nsmutablearray alloc] initwithcapacity:0]; for (int i = 1; i < [arr count]; ++i) {NSString *str = [arr objectatindex:i]; [_array ADDOBJECT:STR]; } [_array componentsjoinedbystring:@ "&"]; NSData *data = [[_array componentsjoinedbystring:@ ' &] datausingencoding:nsasciistringencoding]; [Request Sethttpbody:data];
//Send request and get server feedback data nsdata *urldata = [Aesencdecviewcontroller Get:url];    The first conversion of NSData data to char* string char * test = (char*) [urldata bytes]; std::string old = Deaes (test);

//Second conversion nsdata to UTF8 encoded nsstring to char* string
//NSString *desstr = [[NSString alloc] Initwithdata:urldata encoding:nsutf8stringencoding];
//const char *desresult = [Desstr utf8string];
//std::string old = Deaes (Desresult);
    
//Decrypt Word string to clear text
nsstring *oldstr = [[NSString alloc] InitWithCString:old.c_str ()];

Many times the software read the Chinese page encoding set is gb2312, so the display is garbled. You need to convert the NSString text encoding
 2 nsdata *data = [NSData Datawithcontentsofurl:url];  
3 nsstringencoding enc = Cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);
4 nsstring *retstr = [[NSString alloc] Initwithdata:data ENCODING:ENC];

Verbose Code

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[] = {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];

3,nsdata and UIImage
NSData-UIImage
UIImage *aimage = [UIImage imagewithdata:imagedata];
//Example: Take a picture from a 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,nsdata merged into a 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 { /c0>
//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] initwithcontentsoffile:mp3path2];
//Merge audio
nsmutabledata *sounds = [Nsmutabledata alloc];
[Sounds appenddata:sound1data];
[Sounds appenddata:sound2data];
//Save audio
 
NSLog (@ "Data length:%d", [sounds length]);
 
[Sounds writetofile:[self filepathwithname:@ "Tmp.mp3"] atomically:yes];
         
[window makekeyandvisible];
     
return YES;
 }






Object-c data type conversion NSData turn Nsstring,byte,uiimage

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.