1

I have an NSString that contains a string with UTF8 characters such as: "test \u00e8" <- note that having a string containing this is different from having:

NSString *a = @"test \u00e8";

It's instead equal to have a string such as:

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

So.. Obviously with [NSString stringWithUTF8String:...] I can't obtain the desired string that is: "test è".

Is there a way to convert my string and make readable utf8 characters?

xspecial
  • 537
  • 1
  • 6
  • 17
  • 2
    possible duplicate of [Converting escaped UTF8 characters back to their original form in iOS/Objective C](http://stackoverflow.com/questions/7860867/converting-escaped-utf8-characters-back-to-their-original-form-in-ios-objective) – rob mayoff Oct 03 '12 at 00:05

2 Answers2

0

I think you're going to have to write something yourself, on the grounds that \uabcd is normally parsed by the compiler at compile time, rather than by NSString. Luckily I don't think it's that difficult.

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);
}
Tommy
  • 99,986
  • 12
  • 185
  • 204
0

Awesome! It worked. There was some little syntax error so this is the correct code:

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;

Thank you very much!

xspecial
  • 537
  • 1
  • 6
  • 17
  • You can do this in just one line: `return [NSString stringWithCString:[str cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding];` – rob mayoff Oct 14 '12 at 04:27