0

I've got a custom C++ string class in my iOS app that is based on an array of wchar_t. I have a method to convert it to NSString that looks like this:

NSString *str = [[[NSString alloc]initWithBytes:(const void *)mArray
      length:sizeof(wchar_t) * mLength 
      encoding:NSUTF32LittleEndianStringEncoding]autorelease];
return str;

I've got a case where the input string contains some Chinese characters (specifically 新譯本), and initWithBytes returns nil. There is nothing in the documentation about the method returning nil; is this an error condition? Does anyone know what kind of thing might make the method kick back a nil result?

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • Could you show a small self-contained example? I have tested your code with `wchar_t * mArray = L"新譯本";` and got no problems. – Martin R Aug 13 '13 at 20:58
  • @MartinR - It's sorted now. The `wchar` array that was passed in sometimes had UTF-16 characters added in, and the two encodings handle multibyte characters differently. Apparently if `initWithBytes` gets invalid data it returns nil. I'll post an answer when I'm allowed to. – Tom Hamming Aug 13 '13 at 21:49
  • @Mr.Jefferson : now you can post it as answer... – Fahim Parkar Feb 12 '14 at 14:30
  • @FahimParkar - Right you are. Done. – Tom Hamming Feb 12 '14 at 16:22
  • I strongly recommend using UTF-8 and avoiding wchar_t if at all possible. The C and C++ language don't even specify what encoding is used for wchar_t, it could be 16 or 32 bits, it could be bigendian or littleendian. UTF-8 avoids all these problems. – gnasher729 Oct 24 '14 at 10:50

1 Answers1

0

It turns out that the wchar array that was passed in sometimes had UTF-16 characters in it, and UTF-16 encodes multibyte characters differently. And if initWithBytes gets invalid data, it returns nil.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145