Conversion between NSString, NSData, char* types-Standby

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 the request and get the 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);

The second conversion nsdata to the char* string in the UTF8 encoded NSString
NSString *desstr = [[NSString alloc] Initwithdata:urldata encoding:nsutf8stringencoding];
const char *desresult = [Desstr utf8string];
std::string old = Deaes (Desresult);

Decrypting a character string into 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
1 Nsurl *url = [Nsurl urlwithstring:urlstr];
2 NSData *data = [NSData Datawithcontentsofurl:url];
3 Nsstringencoding enc = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);
4 NSString *retstr = [[NSString alloc] Initwithdata:data Encoding:enc];

Conversion between NSString, NSData, char* types-Standby

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.