#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
//#include <tchar.h>
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len);
void c2w(wchar_t *pwstr,size_t len,const char *str);
int main(int argc, char* argv[])
{
wchar_t pwstr[] =L"我是中国人";
printf("===============");
wprintf(L"原始数据 pwstr:%s",pwstr);
wchar_t pwstr2[20];
char *pcstr = (char *)malloc(sizeof(char)*(2 * wcslen(pwstr)+1));
memset(pcstr , 0 , 2 * wcslen(pwstr)+1 );
w2c(pcstr,pwstr,2 * wcslen(pwstr)+1) ;
printf("wchar=>char result:%s\n",pcstr);
c2w(pwstr2,20,pcstr);
wprintf(L"%s",pwstr2);
free(pcstr) ;
return 0;
}
//将wchar_t* 转成char*的实现函数如下:
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nlength=wcslen(pwstr);
//获取转换后的长度
int nbytes = WideCharToMultiByte( 0, // specify the code page used to perform the conversion
0, // no special flags to handle unmapped characters
pwstr, // wide character string to convert
nlength, // the number of wide characters in that string
NULL, // no output buffer given, we just want to know how long it needs to be
0,
NULL, // no replacement character given
NULL ); // we don't want to know if a character didn't make it through the translation
// make sure the buffer is big enough for this, making it larger if necessary
if(nbytes>len) nbytes=len;
// 通过以上得到的结果,转换unicode 字符为ascii 字符
WideCharToMultiByte( 0, // specify the code page used to perform the conversion
0, // no special flags to handle unmapped characters
pwstr, // wide character string to convert
nlength, // the number of wide characters in that string
pcstr, // put the output ascii characters at the end of the buffer
nbytes, // there is at least this much space there
NULL, // no replacement character given
NULL );
return pcstr ;
}
//将char* 转成wchar_t*的实现函数如下:
//这是把asii字符转换为unicode字符,和上面相同的原理
void c2w(wchar_t *pwstr,size_t len,const char *str)
{
if(str)
{
size_t nu = strlen(str);
size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,NULL,0);
if(n>=len)n=len-1;
MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,pwstr,(int)n);
pwstr[n]=0;
}
}
//或者用此种方法更好一些:============我自已做的
//把ascii 字符转换为unicode字符
//wchar_t* Cphone_hq::ctow(wchar_t *pwstr, const char *str)
//{
//wchar_t* buffer;
//if(str)
// {
// size_t nu = strlen(str);
// size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,int(nu),NULL,0);
// buffer=0;
// buffer = new wchar_t[n+1];
// //if(n>=len) n=len-1;
// ::MultiByteToWideChar(CP_ACP,0,(const char *)str,int(nu),buffer,int(n));
//
// }
//return buffer;
//delete buffer;
//}
分享到:
相关推荐
这个问题困惑了我很长时间,总想理一下确一直没理。 在网上找了个代码,自己测试了下,感觉还行,发上来,希望对和我一样的菜鸟们有点帮助。高手可以不看了。 QQ742627335
使用C++,实现char与wchar_t互转,w2c,c2w分别实现wchar_t到char的转换和char到wchar_t的转换
char 转wchar_t 及wchar_t转char的实现函数及原理说明
实现char wchar_t 之间的转换程序
wchar_t*类型转换为char*类型
介绍char_TCHAR_WCHAR三种类型的区别
CString、LPCTSTR、LPTSTR、TCHAR、WCHAR、string、wchar_t、char解析 CString、LPCTSTR、LPTSTR、TCHAR、WCHAR、string、wchar_t、char解析 CString、LPCTSTR、LPTSTR、TCHAR、WCHAR、string、wchar_t、char解析
UNICODE下宽字符的CString转换为const char 和char到WCHAR的相互转换
VC++中的char,wchar_t,TCHAR,主要讲解char,wchar_t,TCHAR的用法及区别,适合初学者了解相关内容
详细的介绍了char wchar TCHAR的联系和区别,非常适合初学者了解
1、区别wchar_t,char,WCHAR ANSI:即 char,可用字符串处理函数:strcat( ),strcpy( ), strlen( )等以str打头的函数。 UNICODE:wchar_t是Unicode字符的数据类型,它实际定义在里: typedef unsigned short wchar_t;...
详细介绍了VC++中的char wchar_T和TCHAR的含义及区别,对于想了解各种编码的初学者有着较大用处
当在 windows 下开发程序库时,使用 TCHAR 类型,比直接使用 char 和 wchar_t 具有更好的移植性。但是在编写动态链接库时,不能直接导出 TCHAR 类型的函数,给 TCHAR 类型函数的使用带来了一些限制。使用 texport 在...
texport用于自动生成 TCHAR 类型程序的 char 和 wchar_t 类型的函数调用存根
代码如下: #ifndef USE_H_ #define USE_H_ #include #include #include using namespace std; class CUser { public: CUser();... char* WcharToChar(const wchar_t* wp); char* StringToChar
在处理中文时有时需要进行wchar_t,char,string,wstring之间的转换。 其中char和string之间、wchar_t和wstring之间的转换较为简单,代码在vs2010下测试通过。 代码如下:#include <iostream>#include <string>#...
关于char,_wchar_t,_TCHAR,__T(),L,宏__T、TEXT,_TEXT、L.
wchar_t c++ wchar_t处理和socket资料 c++ wchar_t处理和socket资料
关于char, wchar_t, TCHAR, _T(),L,宏 _T、TEXT,_TEXT、L