-->

NSString to wchar_t*?

2020-02-11 06:39发布

问题:

I can't seem to find a good built-in way to convert an NSString to wchar_t... any tips on how to do this? Anything Apple exposes on the NSString class is for c-strings (char*) or for unichars, but nothing for wchar*.

回答1:

[urString cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]

wchar_t is compiler specific. Assuming you wan the Win32 wchar_t, then UTF16 little ending encoded will do it. For Unix wchar_t you may need the NSUTF32 encoding family, with your platform of choice endianess.



回答2:

Following code worked for me: (not sure if this will be applicable for wchar_t as well)

NSString *pStr = [NSString stringWithFormat:@"%S", GetTCHARString()];