1

The following lines of code running with Visual Studio 2013:

wchar_t test[] = L"\xffff";
wchar_t buf[100];    
int ret = swprintf(&buf[0], 100, L"%ls", &test[0]);

It compiles successfully, but swprintf fails to output this character (res is -1). It's ok with any other character except \xffff. This character can be in any position of the string, and swprintf still fails. What's the problem?

muzdima
  • 23
  • 1
  • 4
  • 1
    You will need to fix the error. – nvoigt Aug 07 '14 at 17:38
  • 1
    Whenever you're asking about an error message, you need to include the exact text of the error message in your question. – Keith Thompson Aug 07 '14 at 17:54
  • It's not a valid Unicode string if it contains the code point U+FFFF. See [Can a valid Unicode string contain FFFF](http://stackoverflow.com/questions/3482683/can-a-valid-unicode-string-contain-ffff-is-java-characteriterator-broken). – Jongware Aug 07 '14 at 18:09
  • Is the output being save to a file such that you hex-dump it and know that the function failed/succeeded to output? Or are you looking at a screen with textual output - in this case how do you know `'\xffff'` must print something that is visible? – chux - Reinstate Monica Aug 07 '14 at 18:21

1 Answers1

3

MSVC/MSVCRT has 16-bit wchar_t and thus uses 0xffff as the value of WEOF, thereby precluding it from being a valid character and necessitating that it cause an error when used like this. This does not really preclude use of a Unicode character, since U+FFFF is a non-character in Unicode, but it's still rather undesirable behavior since UTFs are supposed to handle all Unicode scalar values, not just characters.

The only "fix" is to use a proper implementation with 32-bit wchar_t (I think cygwin should work on windows but I'm not sure; otherwise, any unix-like system).

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711