Question about NSString encoding

Discussion in 'iOS Programming' started by Clouds, Jan 22, 2011.

  1. macrumors newbie

    Joined:
    Oct 5, 2010
    #1
    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:
    NSStringescaped_value = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)stringNULL, (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!
     
  2. thread starter macrumors newbie

    Joined:
    Oct 5, 2010
    #2
    Nevermind, I can do it by myself:

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

Share This Page