View Full Version : Question about NSString encoding

Jan 22, 2011, 03:00 PM
I have a question about encoding a NSString
I want to convert this string:

to this encoded string:
(don't know what encoding code is this)

but I don't know how to do.

I also tested with this method:

NSString* escaped_value = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)string, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);

but it returns a string like this:


which doesn't satisfy my need.
There's another issue with this method that it doen't keep the format of the text, it remove all 'newline \n' character so the encoded string returned in one line only. How can I solve that?


Jan 23, 2011, 08:26 AM
Nevermind, I can do it by myself:

NSString *string = @"寫在2011的開端";
NSData *data = [string dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", encodedString);
[encodedString release];