Question

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);
Was it helpful?

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);

OTHER TIPS

You can get the UTF8 representation of any NSString by sending it a -UTF8String message.

And if you have a NSString, you can just do

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

Or, if you needed a const char * (which is unlikely), you could use:

const char *anotherString = [string UTF8String];

Now, if this NSString is something you originally retrieved from your server in the form of a NSData, you'd convert it from the NSData you got from your network request to a string with something like:

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

In this case, though, you'd have to confirm what you mean by the fact that the server used "unicode encoding". That is ambiguous, and you have to confirm if it's UTF8 (which is what I used above), or UTF16 (and if UTF16, which one, e.g. NSUTF16StringEncoding or NSUTF16LittleEndianStringEncoding or ...) or whatever.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top