问题描述
我得到argv为wchar_t **(见下文),因为我需要使用unicode,但我需要将其转换为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 that?
int wmain(int argc, wchar_t** argv)
{
推荐答案
有多种方法可以做到这一点。根据您的环境和可用的编译器/标准库/其他库,您至少有三个选择:
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:
- 使用std :: locale和std :: codecvt<> facet;
- 使用C语言环境函数,如std :: mbstowcs();
- 使用第三方函数,如iconv * nix或WideCharToMultiByte()在Windows上。
你真的需要做转换吗?
你应该意识到,经常(特别是在Windows上)从wchar_t字符串到char字符串的转换是有损的转换。字符串使用的字符集通常不是UTF-8。例如。如果将带有国家字符或亚洲语言的文件名转换为字符串,那么您最有可能得到的东西将无法真正用于访问原始文件。
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.
这篇关于如何转换wchar_t **到char **?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!