wchar_t
is a 32-bit integer (on iOS and OS X), therefore you must use NSUTF32LittleEndianStringEncoding
for the conversion to NSString
.
Example:
std::wstring val (L" Hello World ");
NSString *initial = [[NSString alloc] initWithBytes:val.data() length:sizeof (wchar_t)*val.size() encoding:NSUTF32LittleEndianStringEncoding];
NSString *trimmed = [initial stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"'%@'", trimmed);
// Output: 'Hello World'
What probably happened in your case (with NSUTF16LittleEndianStringEncoding
)
is that every second character in the initial
string is a NUL character,
which acts as a terminator when printed.