0

I'm attempting to convert a wchar_t* to a char*. Here's my code:

size_t result = wcstombs(returned, str, length + 1);
if (result == (size_t)-1) {
    int error = errno;
}

It indeed fails, and error is filled with 92 (ENOPROTOOPT) - Protocol not available.

I've even tried setting the locale:

setlocale(LC_ALL, "C");

And this one too:

setlocale(LC_ALL, "");

I'm tempted to just throw the characters with static casts!

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Mike Weir
  • 3,094
  • 1
  • 30
  • 46
  • 2
    The locale will only affect the *result* of the conversion. In any event, you have to make sure that the source wide string is encoded in "the system's wide encoding", which you generally don't know nor have any control over. Basically, the wide string should be the result of a `mbstowcs()` call, or an `iconv()` conversion to WCHAR_T. [Here is a question of mine](http://stackoverflow.com/questions/6300804/wchars-encodings-standards-and-portability) on this subject. – Kerrek SB Dec 06 '11 at 12:55

1 Answers1

0

Seems the issue was that the source string was encoded with a non-standard encoding (two ASCII characters for each wide character), which looked fine in the debugger, but clearly internally was sour. The error code produced is clearly not documented, but it's the equivalent to simply not being able to decode said piece of text.

Mike Weir
  • 3,094
  • 1
  • 30
  • 46