WCHAR和char的转换

#include <windows.h>  
#include <stdio.h>  
  
//function: charTowchar  
//purpose:char to WCHAR 、wchar_t、LPWSTR etc  
void charTowchar(const char *chr, wchar_t *wchar, int size)  
{     
    MultiByteToWideChar( CP_ACP, 0, chr,  
        strlen(chr)+1, wchar, size/sizeof(wchar[0]) );  
}  
  
//function: wcharTochar  
//purpose:WCHAR 、wchar_t、LPWSTR to char  
void wcharTochar(const wchar_t *wchar, char *chr, int length)  
{  
    WideCharToMultiByte( CP_ACP, 0, wchar, -1,  
        chr, length, NULL, NULL );  
}  
  
int main (void)  
{  
    char     chr[128];  
    wchar_t  *wchar = L"陈鸿钦";        
  
    //wchar_t to char  
    wcharTochar(wchar, chr, sizeof(chr));  
    printf("char is %s\n", chr);  
  
    //char to wchar_t  
    wchar = (wchar_t *)malloc(sizeof(wchar_t) * 64);  
    charTowchar(chr, wchar, sizeof(wchar_t) * 64);  
      
    wprintf_s(L"%s\n", wchar);//  
    getchar();  
  
    return 0;  
}  

你可能感兴趣的:(WCHAR和char的转换)