5

I get argv as wchar_t** (see below), because I need to work with unicode, but I need to convert it to char **. How can I do that?

int wmain(int argc, wchar_t** argv) 
{ 
Narek
  • 38,779
  • 79
  • 233
  • 389
  • 3
    You don't :-p I'm strongly in favour of doing all your work in Unicode when that's what you were given. You just need to allocate `char** myArgs = new char*[argc];` and then loop through argv converting each string at a time then filling in `myArgs` with the converted string - I expect someone will give you code for this which is why I'm not posting an answer. But please consider going the other way - modify the code that's consuming the char** to accept wchar_t** instead. – Rup Jul 12 '10 at 09:36
  • 2
    The conversion, since I forgot to mention it, is done with `WideCharToMultiByte` or `wcstombs` (prefer the `_s` version). But you need to allocate a buffer to receive the string and potentially resize that if you got it wrong - I didn't write code because I can't remember if there's a neat / simple way to do that. Easiest to just aim high I guess. – Rup Jul 12 '10 at 09:43
  • Thanks that you talk about things that you precisely know, and not pretend that you know and can everything. So +2 for your modesty! – Narek Jul 12 '10 at 09:52
  • If you need to work with Unicode, you should _not_ convert to `char*`, period. – sbi Jul 12 '10 at 11:14

4 Answers4

4

There is more than one way to do this. Depending on your environment and available compiler/standard library/other libraries, you have at least three choices:

  1. Use std::locale and std::codecvt<> facet;
  2. use C locale functions like std::mbstowcs();
  3. use 3rd party functions like iconv() on *nix or WideCharToMultiByte() on Windows.

Do you really need to do the conversion?

You should realise that often (especially on Windows) the conversion from wchar_t string to char string is a lossy conversion. The character set used by system for char strings is often not UTF-8. E.g. if you convert a file name with national characters or in some Asian language to char string you are most likely going to get something that will not be really usable to access the original file.

wilx
  • 17,697
  • 6
  • 59
  • 114
2

This does the trick:

#define MAXLEN 512
   char tx[MAXLEN];
   mbstowcs(argv[i], tx, MAXLEN);
RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92
0

Why do you need to convert? In most of the cases you need to change your project settings, so everything accepts wide chars. If a third party library wants non-Unicode string, then you need to recompile it with the proper options for Unicode. If there are no proper options for Unicode, I would get rid of this library and find (write) a better one.

m_pGladiator
  • 8,462
  • 7
  • 43
  • 61
0

I needed this for a hack to get ancient code working. It is not thread safe because it is returning a pointer to an internal static array. @red-soft-adair answer converts in the opposite direction.

char *WideStringToCString(wchar_t *wcStr)
{
#define MAXLEN 512

    static char tx[MAXLEN];
    wcstombs(tx, wcStr, MAXLEN-1);
    tx[MAXLEN - 1] = 0;
    return tx;
}
trindflo
  • 311
  • 3
  • 12