views:

212

answers:

2

Hi , i have NSstring that contain encoded utf8 i want to decode it.

how can i do that?

i try this but it didn't work

NSString *decodedString = [NSString stringWithUTF8String:[encodedString cStringUsingEncoding:[NSString defaultCStringEncoding]]];

thanks

+1  A: 

Look at the documentation for NSString. You'll find what you're looking for.

Jasarien
It might help to [link to that documentation](http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html).
Peter Hosey
+7  A: 

Sending cStringUsingEncoding: to encodedString can only work if encodedString is an NSString object. Having an NSString object is the point you're trying to get to. Moreover, that method does not decode the string, it encodes the string using the encoding you ask for; thus, in the code you show, you ask for the (already-encoded) string to be encoded in some random encoding (the default C string encoding), then attempt to decode the result as UTF-8. That won't work.

You don't specify what type you're using for encodedString. Either way, you need to create the NSString object by passing encodedString and its encoding.

All of these except initWithUTF8String: require you to specify the encoding (initWithUTF8String: does by definition), and all of them except initWithData:encoding: come in an autoreleasing convenience version (+stringWith… instead of -initWith…).

Peter Hosey