如何从LPWSTR转换为const char*

时间:2021-05-21 20:12:38

After getting a struct from C# to C++ using C++/CLI:

使用c++ /CLI从c#到c++的struct:

public value struct SampleObject
{   
    LPWSTR a;    
};

I want to print its instance:

我想打印它的实例:

printf(sampleObject->a);

but I got this error:

但我犯了一个错误:

Error 1 error C2664: 'printf' : cannot convert parameter 1 from 'LPWSTR' to 'const char *'

错误1错误C2664:“printf”:不能将参数1从“LPWSTR”转换为“const char *”

How can I convert from LPWSTR to char*?

如何从LPWSTR转换为char*?

Thanks in advance.

提前谢谢。

5 个解决方案

#1


3  

Don't convert.

不转换。

Use wprintf instead of printf:

使用wprintf代替printf:

See the examples which explains how to use it.

请参见说明如何使用它的示例。

Alternatively, you can use std::wcout as:

或者,您可以使用std::wcout:

wchar_t *wstr1= L"string";
LPWSTR   wstr2= L"string"; //same as above
std::wcout << wstr1 << L", " << wstr2;

Similarly, use functions which are designed for wide-char, and forget the idea of converting wchar_t to char, as it may loss data.

类似地,使用为宽字符设计的函数,忘记将wchar_t转换为char的想法,因为它可能丢失数据。

Have a look at the functions which deal with wide-char here:

看看这里处理宽字符的函数:

#2


15  

Use the wcstombs() function, which is located in <stdlib.h>. Here's how to use it:

使用wcstombs()函数,该函数位于 中。下面是如何使用它:

LPWSTR wideStr = L"Some message";
char buffer[500];

// First arg is the pointer to destination char, second arg is
// the pointer to source wchar_t, last arg is the size of char buffer
wcstombs(buffer, wideStr, 500);

printf("%s", buffer);

Hope this helped someone! This function saved me from a lot of frustration.

希望这帮助别人!这个功能使我避免了很多挫折。

#3


4  

Just use printf("%ls", sampleObject->a). The use of l in %ls means that you can pass a wchar_t[] such as L"Wide String".

只使用printf(" % ls”,sampleObject - >)。l在%ls中的使用意味着您可以通过一个wchar_t[],例如l“宽字符串”。

(No, I don't know why the L and w prefixes are mixed all the time)

(不,我不知道为什么L和w的前缀总是混合在一起)

#4


2  

int length = WideCharToMultiByte(cp, 0, sampleObject->a, -1, 0, 0, NULL, NULL);
char* output = new char[length];
WideCharToMultiByte(cp, 0, sampleObject->a, -1, output , length, NULL, NULL);
printf(output);
delete[] output;

#5


0  

use WideCharToMultiByte() method to convert multi-byte character.

使用WideCharToMultiByte()方法转换多字节字符。

Here is example of converting from LPWSTR to char* or wide character to character.

这里是从LPWSTR转换为char*或宽字符到字符的示例。

/*LPWSTR to char* example.c */
#include <stdio.h>
#include <windows.h>

void LPWSTR_2_CHAR(LPWSTR,LPSTR,size_t);

int main(void)
{   
    wchar_t w_char_str[] = {L"This is wide character string test!"};
    size_t w_len = wcslen(w_char_str);
    char char_str[w_len + 1];
    memset(char_str,'\0',w_len * sizeof(char));
    LPWSTR_2_CHAR(w_char_str,char_str,w_len);

    puts(char_str);
    return 0;
}

void LPWSTR_2_CHAR(LPWSTR in_char,LPSTR out_char,size_t str_len)
{   
    WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,in_char,-1,out_char,str_len,NULL,NULL);
}

#1


3  

Don't convert.

不转换。

Use wprintf instead of printf:

使用wprintf代替printf:

See the examples which explains how to use it.

请参见说明如何使用它的示例。

Alternatively, you can use std::wcout as:

或者,您可以使用std::wcout:

wchar_t *wstr1= L"string";
LPWSTR   wstr2= L"string"; //same as above
std::wcout << wstr1 << L", " << wstr2;

Similarly, use functions which are designed for wide-char, and forget the idea of converting wchar_t to char, as it may loss data.

类似地,使用为宽字符设计的函数,忘记将wchar_t转换为char的想法,因为它可能丢失数据。

Have a look at the functions which deal with wide-char here:

看看这里处理宽字符的函数:

#2


15  

Use the wcstombs() function, which is located in <stdlib.h>. Here's how to use it:

使用wcstombs()函数,该函数位于 中。下面是如何使用它:

LPWSTR wideStr = L"Some message";
char buffer[500];

// First arg is the pointer to destination char, second arg is
// the pointer to source wchar_t, last arg is the size of char buffer
wcstombs(buffer, wideStr, 500);

printf("%s", buffer);

Hope this helped someone! This function saved me from a lot of frustration.

希望这帮助别人!这个功能使我避免了很多挫折。

#3


4  

Just use printf("%ls", sampleObject->a). The use of l in %ls means that you can pass a wchar_t[] such as L"Wide String".

只使用printf(" % ls”,sampleObject - >)。l在%ls中的使用意味着您可以通过一个wchar_t[],例如l“宽字符串”。

(No, I don't know why the L and w prefixes are mixed all the time)

(不,我不知道为什么L和w的前缀总是混合在一起)

#4


2  

int length = WideCharToMultiByte(cp, 0, sampleObject->a, -1, 0, 0, NULL, NULL);
char* output = new char[length];
WideCharToMultiByte(cp, 0, sampleObject->a, -1, output , length, NULL, NULL);
printf(output);
delete[] output;

#5


0  

use WideCharToMultiByte() method to convert multi-byte character.

使用WideCharToMultiByte()方法转换多字节字符。

Here is example of converting from LPWSTR to char* or wide character to character.

这里是从LPWSTR转换为char*或宽字符到字符的示例。

/*LPWSTR to char* example.c */
#include <stdio.h>
#include <windows.h>

void LPWSTR_2_CHAR(LPWSTR,LPSTR,size_t);

int main(void)
{   
    wchar_t w_char_str[] = {L"This is wide character string test!"};
    size_t w_len = wcslen(w_char_str);
    char char_str[w_len + 1];
    memset(char_str,'\0',w_len * sizeof(char));
    LPWSTR_2_CHAR(w_char_str,char_str,w_len);

    puts(char_str);
    return 0;
}

void LPWSTR_2_CHAR(LPWSTR in_char,LPSTR out_char,size_t str_len)
{   
    WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,in_char,-1,out_char,str_len,NULL,NULL);
}