HurkNburkS
HurkNburkS

Reputation: 5510

Convert CString to NSUTF16encoded NSString

I have a cstring, which I am able to convert into a NSUTF8 NSString like this

NSString *resultString = [[NSString alloc] initWithUTF8String:cstring];

however I was wondering if there is a way to convert it into a UTF16String?

Upvotes: 0

Views: 234

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130183

You can simply use NSString's stringWithCString:encoding

NSString *resultString = [NSString stringWithCString:cstring encoding:NSUTF16StringEncoding];

Or if you wish to stick with the alloc/init format use:

NSString *resultString = [[NSString alloc] initWithCString:cstring encoding:NSUTF16StringEncoding];

Upvotes: 2

Related Questions