源头是百度博客一大仙儿的,经过自己几次改进,应该没有问题了
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
size_t get_wchar_size(const char *str)
{
size_t len = strlen(str);
size_t size=0;
size_t i;
for(i=0; i < len; i++)
{
if( str[i] >= 0 ) //不是全角字符
size+=sizeof(wchar_t);
else //是全角字符,是中文
{
size+=sizeof(wchar_t);
i+=1;
}
}
return size;
}
char *w2c(const wchar_t *pw)
{
if(!pw)
return NULL;
size_t size= wcslen(pw)*sizeof(wchar_t) + sizeof(char);
char *pc;
if(!(pc = (char*)malloc(size)))
{
printf("malloc fail");
return NULL;
}
memset(pc, 0, size);
wcstombs(pc,pw,size);
return pc;
}
wchar_t *c2w(const char *pc)
{
if(!pc)
return NULL;
size_t size_of_ch = strlen(pc)*sizeof(char);
size_t size_of_wc = get_wchar_size(pc) + sizeof(wchar_t);
wchar_t *pw;
if(!(pw = (wchar_t*)malloc(size_of_wc)))
{
printf("malloc fail");
return NULL;
}
memset(pw, 0, size_of_wc);
mbstowcs(pw,pc,size_of_wc);
return pw;
}
int main(void)
{
setlocale(LC_ALL,"zh_CN.utf8");
printf("1 print chinese by wprintf test: \n");
wchar_t *wstr = L"中文";
//wprintf(L"%ls",wstr);
printf("%ls\n",wstr); //ls
printf("2 print chinese by printf test: \n");
char *str = "汉字";
printf("%s\n\n\n",str); //s
printf("3 char and wchar_t size of system test: \n");
printf("%s%d\n","the size of wchar_t is : ",sizeof(wchar_t)); //4
printf("%s%d\n\n\n","the size of char is : ",sizeof(char)); //1
char *pc;
wchar_t *pw = L"中文是abc一门语言abc";
char *tmp = "中文是abc一门语言abc";
printf("%s%s\n","input test wchar_t* ",tmp);
pc = w2c(pw);
printf("4 print char test after w2c (wchar to char):\n");
printf("%s\n",pc);
wchar_t *cw1 = c2w(pc); //char to wchar
char *wc1 = w2c(cw1); //wchar to char
printf("5 print char test after w2c c2w and w2c:\n");
printf("%s\n\n\n",wc1);
char *pmc = "abc中文abc";
wchar_t *pmw;
printf("%s%s\n","input test char* ",pmc);
pmw = c2w(pmc);
char *pmc1 = w2c(pmw);
printf("6 print char test after c2w and w2c:\n");
printf("%s\n",pmc1);
free(pc);
free(cw1);
free(wc1);
return 0;
}
源贴: http://hi.baidu.com/yangyangye2008/blog/item/fcb0943115fc7294a8018e15.html#0
tchar <---> char(utf-8/ansi(gb2312))
上述方法在linux下正常,到windows下wcstombs总是返回-1,setlocal也老失败(2011-10-25)
用下面的方式正常(已经过测试并用到工作项目中了,暂时没发现问题)
/************************************************************************/
/* convert Utf8 char to Unicode/LocalCode TCHAR */
/************************************************************************/
TCHAR* ConvertUtf8ToTChar(const char* src)
{
uint32 size = strlen(src)+1;
WCHAR* dest = new WCHAR[size];
memset( dest, 0, sizeof(WCHAR)*size );
MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, (int)size);
#ifdef UNICODE
return dest;
#else
//convert unicode to local code.
uint32 size_local = wcslen(dest) + 1;
char* dest2 = new char[size_local];
WideCharToMultiByte(CP_ACP, 0, dest, -1, dest2, size, NULL, NULL);
delete[] dest;
return dest2;
#endif
}
/************************************************************************/
/* convert Unicode/LocalCode TCHAR to Utf8 char */
/************************************************************************/
char* ConvertTCharToUtf8(const TCHAR* src)
{
#ifdef UNICODE
WCHAR* tmp = (WCHAR*)src;
int32 size = wcslen(src)*3 + 1;
char* dest = new char[size];
memset( dest, 0, size );
WideCharToMultiByte(CP_UTF8, 0, tmp, -1, dest, size, NULL, NULL);
return dest;
#else
char* tmp = (char*)src;
uint32 size = strlen(tmp)+1;
WCHAR* dest = new WCHAR[size];
memset( dest, 0, sizeof(WCHAR)*size );
MultiByteToWideChar(CP_ACP, 0, src, -1, dest, (int)size); // convert local code to unicode.
size = wcslen(dest)*3 + 1;
char* dest2 = new char[size];
memset( dest2, 0, size );
WideCharToMultiByte(CP_UTF8, 0, dest, -1, dest2, size, NULL, NULL); // convert unicode to utf8.
delete[] dest;
return dest2;
#endif
}
/************************************************************************/
/* convert Unicode/LocalCode CString to LocalCode string */
/************************************************************************/
void ConvertCStringToLocalString(CString& srcCstring, string& destString)
{
srcCstring.LockBuffer();
TCHAR* src = srcCstring.GetBuffer();
#ifdef UNICODE
WCHAR* tmp = (WCHAR*)src;
int32 size = wcslen(src)*3 + 1;
char* dest = new char[size];
memset( dest, 0, size );
WideCharToMultiByte(CP_ACP, 0, tmp, -1, dest, size, NULL, NULL);
destString = string(dest);
delete[] dest;
#else
char* tmp = (char*)src;
destString = string(tmp);
#endif
srcCstring.ReleaseBuffer();
srcCstring.UnlockBuffer();
}
/************************************************************************/
/* convert Unicode/LocalCode CString to Utf8 string */
/************************************************************************/
void ConvertCStringToUtf8String(CString& srcCstring, string& destString)
{
srcCstring.LockBuffer();
TCHAR* src = srcCstring.GetBuffer();
#ifdef UNICODE
WCHAR* tmp = (WCHAR*)src;
int32 size = wcslen(src)*3 + 1;
char* dest = new char[size];
memset( dest, 0, size );
WideCharToMultiByte(CP_UTF8, 0, tmp, -1, dest, size, NULL, NULL);
destString = string(dest);
delete[] dest;
#else
char* tmp = (char*)src;
uint32 size = strlen(tmp)+1;
WCHAR* dest = new WCHAR[size];
memset( dest, 0, sizeof(WCHAR)*size );
MultiByteToWideChar(CP_ACP, 0, src, -1, dest, (int)size); // convert local code to unicode.
size = wcslen(dest)*3 + 1;
char* dest2 = new char[size];
memset( dest2, 0, size );
WideCharToMultiByte(CP_UTF8, 0, dest, -1, dest2, size, NULL, NULL); // convert unicode to utf8.
delete[] dest;
destString = string(dest2);
delete[] dest2;
#endif
srcCstring.ReleaseBuffer();
srcCstring.UnlockBuffer();
}