Encoding and decoding using UTF-8 on iPhone

I am looking for an example demonstrating how I can encode and then decode the same string using UTF-8. Encoding and then Decoding means that I want to implement methods in 2 areas where they can be encoded and the other can be decoded.

I saw the API, but I did not get much success:

  • stringWithCString:encoding:
  • stringWithUTF8String:
  • stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;

EDITED
I have a string øæ-test-2.txtthat I encode as follows:

char *s = "øæ-test-2.txt";
NSString *enc = [NSString stringWithCString:s encoding:NSASCIIStringEncoding];

but I get øæ-test-2.txtas output. Now I want to return the original string back, i.e.øæ-test-2.txt


EDITED
I get øæ-test-2.txtfrom the server, and I need øæ-test-2.txtto decode it. I can get the output from the link: http://www.cafewebmaster.com/online_tools/utf_decode

, , .

, - - , .

+3
2

NSString C- UTF8, UTF8String

char *utf8string = [@"A string with ümläuts" UTF8String];

C- UTF8 NSString, stringWithUTF8String: initWithUTF8String:

NSString *string = [NSString stringWithUTF8String:utf8string];

, NSString UTF-16, "UTF-8 NSString" ( ).

+7

char *utf8string = [@"A string with ümläuts" UTF8String];

const char *utf8string = [@"A string with ümläuts" UTF8String];

.

+2

Source: https://habr.com/ru/post/1785196/


All Articles