XCode - 转换为fly UTF8字符,例如\ u00e8

时间:2012-10-02 22:18:34

标签: iphone objective-c encoding utf-8 character-encoding

我有一个NSString,其中包含一个包含UTF8字符的字符串,例如: “test \ u00e8”< - 请注意,包含此字符串的字符串不同于:

NSString *a = @"test \u00e8";

它等于有一个字符串,如:

NSString *a = @"test \ \ u00e8"; //note the double \\ for escape...

所以..显然[NSString stringWithUTF8String:...]我无法获得所需的字符串:“testè”

有没有办法转换我的字符串并生成可读的utf8字符?

2 个答案:

答案 0 :(得分:0)

我认为您必须自己编写一些内容,理由是\uabcd通常在编译时由编译器解析,而不是NSString。幸运的是,我并不认为这很难。

NSString *fragments = [string componentsSeparatedByString:@"\u"];

if([fragments count])
{
    NSObjectEnumerator *stringEnumerator = [fragments objectEnumerator];
    NSMutableString *decodedString = 
                  [[[stringEnumerator nextObject] mutableCopy] autorelease];

    NSString *nextFragment;
    while(nextFragment = [stringEnumerator nextObject])
    {
         if([nextFragment length] >= 4)
         {
              unichar decodedCharacter = 0;

              for(int c = 0; c < 4; c++)
              {
                  unichar hexValue = [nextFragment characterAtIndex:c];

                  if(hexValue >= 'a')
                      hexValue = 0xa + (hexValue - 'a');
                  else
                      hexValue = hexValue - '0';

                  decodedCharacter = (decodedCharacter << 4) + hexValue;
              }

              [decodedString appendFormat:@"%C", decodedCharacter];
              [decodedString appendString:[nextFragment substringFromIndex:4]];
         }
         else
         {
              // there seems to be a parsing error; maybe just append
              // next fragment?
         }
    }

    NSLog(@"decoded string is %@", decodedString);
}

答案 1 :(得分:0)

真棒!有效。 有一些语法错误,所以这是正确的代码:

NSArray *fragments = [str componentsSeparatedByString:@"\\u"];
if([fragments count])
{
    NSEnumerator *stringEnumerator = [fragments objectEnumerator];
    NSMutableString *decodedString =
    [[stringEnumerator nextObject] mutableCopy];

    NSString *nextFragment;
    while(nextFragment = [stringEnumerator nextObject])
    {
        if([nextFragment length] >= 4)
        {
            unichar decodedCharacter = 0;

            for(int c = 0; c < 4; c++)
            {
                unichar hexValue = [nextFragment characterAtIndex:c];

                if(hexValue >= 'a')
                    hexValue = 0xa + (hexValue - 'a');
                else
                    hexValue = hexValue - '0';

                decodedCharacter = (decodedCharacter << 4) + hexValue;
            }

            [decodedString appendFormat:@"%C", decodedCharacter];
            [decodedString appendString:[nextFragment substringFromIndex:4]];
        }
        else
        {
            // there seems to be a parsing error; maybe just append
            // next fragment?
        }
    }
    return decodedString;
}
return str;

非常感谢!