Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decode UTF8 string in iphone

I want to decode an UT8 encoded string.

The input string is "øæ-test-2.txt"

and after decoding it should become "øæ-test-2.txt"

I found many API to encode the NSString or NSData to UT8 (NSUTF8StringEncoding) but was not able to find the way to decode it.

What I have tried until now:-

NSString *str = [[NSString alloc] initWithUTF8String:[strToDecode cStringUsingEncoding:NSUTF8StringEncoding]];

AND

[strToDecode stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

AND

[NSString stringWithUTF8String:[strToDecode cStringUsingEncoding:[NSString defaultCStringEncoding]]]

I have tried the same input string and I get the proper output in third party decoder.

But was not able to get success

Any hint in right direction would be highly appreciated.

like image 293
Ekra Avatar asked Feb 10 '26 16:02

Ekra


2 Answers

I use this one.

NSString *encoded_string = @"ü";
const char *ch = [encoded_string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *decode_string = [[NSString alloc]initWithCString:ch encoding:NSUTF8StringEncoding];
NSLog(@"%@",decode_String)
like image 139
Phil Avatar answered Feb 12 '26 14:02

Phil


[NSString stringWithUTF8String:]
like image 27
Nickolay Olshevsky Avatar answered Feb 12 '26 15:02

Nickolay Olshevsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!