Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Clouds

macrumors newbie
Original poster
Oct 5, 2010
3
0
Hi,
I have a question about encoding a NSString
I want to convert this string:
寫在2011的開端

to this encoded string:
\U5beb\U57282011\U7684\U958b\U7aef
(don't know what encoding code is this)

but I don't know how to do.

I also tested with this method:

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

but it returns a string like this:

%E5%AF%AB%E5%9C%A82011%E7%9A%84%E9%96%8B%E7%AB%AF

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?

Thanks!
 
Nevermind, I can do it by myself:

PHP:
NSString *string = @"寫在2011的開端";
NSData *data = [string dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", encodedString);
[encodedString release];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.