I have tried to search in Stackoverflow. But I could not get the correct solution. Can someone please help me?
I have this data from a web service.
KEY: 1036 TYPE: string VALUE = French (fran\u00E7aise)
KEY: 1032 TYPE: string VALUE = Greek (\u03B5\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC)
Does anyone know how to print this Unicode string correctly? I have tried to encode and decode it back (as shown below). But it is not working. :(
NSString *value = [NSString stringWithFormat:@"%@", t.dataValue];
NSData *bytes = [message dataUsingEncoding:NSUTF8StringEncoding];
NSString* messageDecoded = [[NSString alloc] initWithData:bytes encoding:NSUTF8StringEncoding];
NSLog(@"decoded: %@", messageDecoded);
Thanks in advance guys!
ANSWER:
I got an answer from this link:
NSString* input = @"ab\"cA\"BC\\u2345\\u0123";
// will cause trouble if you have "abc\\\\uvw"
NSString* esc1 = [input stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString* quoted = [[@"\"" stringByAppendingString:esc2] stringByAppendingString:@"\""];
NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
NSString* unesc = [NSPropertyListSerialization propertyListFromData:data
mutabilityOption:NSPropertyListImmutable format:NULL
errorDescription:NULL];
assert([unesc isKindOfClass:[NSString class]]);
NSLog(@"Output = %@", unesc);