Convert NSString to UTF8 encoded NSSTRING__ encoding

Source: Internet
Author: User

In the use of network address, it is generally necessary to first encode the URL into the UTF8 format of the encoding, or in use may report the URL does not exist errors, then need to convert

Here is the conversion function:

NSString *urlstring= [NSString stringwithformat:@ "http://www.baidu.com];

NSString * encodedstring = (NSString *) cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault, (CFSTRINGREF) URLString, NULL, NULL, KCFSTRINGENCODINGUTF8);

Nsurl *url =[nsurl urlwithstring:encodedstring];


Or use the following method:

NSString UTF8STR = @ "testing";

NSString *unicodestr = [NSString stringwithcstring:[utf8str utf8string] encoding:nsunicodestringencoding];


Sometimes get the URL of the Chinese characters are garbled, the Web content is garbled, need to be transcoded to correctly identify NSString, you can use the following method:

Solve garbled problems ()

NSString *transstring = [NSString stringwithstring:[string stringbyreplacingpercentescapesusingencoding: Nsutf8stringencoding]];

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.