1

I have a strange problem encoding my String

For example:

NSString *str = @"\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13";
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %@", utf);

This worked perfectly in log

utf: ฉันรักคุณ

But, when I try using my string that I parsed from JSON with the same string:

//str is string parse from JSON
NSString *str = [spaces stringByReplacingOccurrencesOfString:@"U" withString:@"u"];
NSLog("str: %@, str);
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %@", utf);

This didn't work in log

str: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
utf: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13

I have been finding the answer for hours but still have no clue

Any would be very much appreciated! Thanks!

3
  • Did you try [str UTF8String]; before NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; ? Commented Mar 4, 2014 at 10:14
  • paste the code which creates the spaces Commented Mar 4, 2014 at 10:16
  • @HimanshuJoshi Yes I have. Didn't work :/ Commented Mar 4, 2014 at 13:41

1 Answer 1

2

The string returned by JSON is actually different - it contains escaped backslashes (for each "\" you see when printing out the JSON string, what it actually contains is @"\").

In contrast, your manually created string already consists of "ฉันรักคุณ" from the beginning. You do not insert backslash characters - instead, @"\u0e09" (et. al.) is a single code point.

You could replace this line

NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

with this line

NSString *utf = str;

and your example output would not change. The stringByReplacingPercentEscapesUsingEncoding: refers to a different kind of escaping. See here about percent encoding.

What you need to actually do, is parse the string for string representations of unicode code points. Here is a link to one potential solution: Using Objective C/Cocoa to unescape unicode characters. However, I would advise you to check out the JSON library you are using (if you are using one) - it's likely that they provide some way to handle this for you transparently. E.g. JSONkit does.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello I have & in my image url so if i pass with service server take it as new parameter so how to solve it. can you please tell. thank you @janis K

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.