1

我有一个NSString包含 UTF8 字符的字符串,例如: “test \u00e8” <- 请注意,包含此字符串的字符串与具有以下内容不同:

NSString *a = @"test \u00e8";

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

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

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

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

4

2 回答 2

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;

非常感谢!

于 2012-10-03T08:33:28.007 回答
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);
}
于 2012-10-02T22:37:23.117 回答