How to convert wchar_t ** to char **?

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 this?

int wmain(int argc, wchar_t** argv) { 
+4
source share
3 answers

There are several ways to do this. Depending on your environment and the compiler / standard library / other libraries available, you have at least three options:

  • Use std :: locale and std :: codecvt <> facet;
  • use C language functions such as std :: mbstowcs ();
  • use third-party functions like iconv () on * nix or WideCharToMultiByte () on Windows.

Do you really need to do the conversion?

You should understand that often (especially on Windows) converting from a wchar_t string to a char string is a lossy conversion. The character set used by the system for char strings is often not UTF-8. For instance. if you convert the file name with national characters or in some Asian language to a char string, most likely you will get something that will not be really useful for accessing the source file.

+4
source

This is the trick:

 #define MAXLEN 512 char tx[MAXLEN]; mbstowcs(argv[i], tx, MAXLEN); 
+1
source

Why do you need to convert? In most cases, you need to change the settings of your project so that everyone accepts wide characters. If a third-party library requires a string other than Unicode, you need to recompile it with the appropriate parameters for Unicode. If there are no suitable parameters for Unicode, I would get rid of this library and find (write) the best.

0
source

Source: https://habr.com/ru/post/1315382/


All Articles