objective-cunicodeencodingutf-8air-native-extension

Objective-C unicode to utf8


I have a NSString in objective-c with unicode encoding and I want to convert it to UTF8. Looks simple but I cannot find a simple way to do it, and there's not so much info on the web.

Basically Im using FRE to send objects to AS3 and I have problems with the encoding. The problem is related to special characters that don't appear on AS3 side.

EDIT:

This is my main problem, I have this name in a NSString Stanisław. And this is the result from a extended method in NSString:

NSLog(@"%@, %d, %s", self, [self length], [self UTF8String]);

//Result: Stanisław, 9, Stanis≈Çaw

So when I move the UTF8String to AS3 I obtain a different name (Basically the result is Stanisła (without the last letter)

The method Im using to convert strings to FRE objects is:

uint32_t st = FRENewObjectFromUTF8([self length], (const uint8_t *)[self UTF8String], &rv);

Solution

  • The problem in

    uint32_t st = FRENewObjectFromUTF8([self length], (const uint8_t *)[self UTF8String], &rv);
    

    is that [self length] is the number of Unicode characters in the string, and not the number of UTF-8 bytes in [self UTF8String].

    In your case "Stanisław" has 9 Unicode characters which are converted to 10 UTF-8 bytes. Your code would send only the first 9 bytes, which is the "shortening" that you observed.

    Therefore you should replace your code with

    const char *utf8 = [self UTF8String];
    uint32_t st = FRENewObjectFromUTF8(strlen(utf8), (const uint8_t *)utf8, &rv);