又陷入了Unicode的泥潭,工作中遇到一個(gè)模塊需要從wchar_t* 轉(zhuǎn)到 char*,而且后續(xù)的工作都是基于Char*進(jìn)行的。網(wǎng)上找了下資料,代碼如下:
還有個(gè)搞人的東西是VS2005下的ifstream及ofstream函數(shù),打開(kāi)帶有中文路徑的文件會(huì)失敗。
解決方案非常HACK,例如
非簡(jiǎn)體中文操作系統(tǒng)如下解決:
std::locale prev_loc = std::locale::global( std::locale("chs") ); // 沒(méi)有這一句的話(huà),文件打開(kāi)失敗
std::ifstream file( "d://測(cè)試//test.txt" );
std::locale::global( prev_loc ); // 沒(méi)有這一句的話(huà),文件中的中文無(wú)法輸出,且wcout輸出中文也失敗
簡(jiǎn)體中文操作系統(tǒng)下,
std::locale::global( std::locale("") ); // 沒(méi)有這一句的話(huà),文件打開(kāi)失敗
std::ifstream file( "d://測(cè)試//test.txt" );
--------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <windows.h>
#include <locale.h>
#define BUFF_SIZE 1024
wchar_t * ANSIToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_ACP, 0, str,-1, NULL,0 );
result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
memset(result,0,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen );
return result;
}
char * UnicodeToANSI( const wchar_t* str )
{
char* result;
int textlen;
textlen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset( result, 0, sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );
return result;
}
wchar_t * UTF8ToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1, NULL,0 );
result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
memset(result,0,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen );
return result;
}
char * UnicodeToUTF8( const wchar_t* str )
{
char* result;
int textlen;
textlen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset(result, 0, sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );
return result;
}
/*寬字符轉(zhuǎn)換為多字符Unicode - ANSI*/
char* w2m(const wchar_t* wcs)
{
int len;
char* buf;
len =wcstombs(NULL,wcs,0);
if (len == 0)
return NULL;
buf = (char *)malloc(sizeof(char)*(len+1));
memset(buf, 0, sizeof(char) *(len+1));
len =wcstombs(buf,wcs,len+1);
return buf;
}
/*多字符轉(zhuǎn)換為寬字符ANSI - Unicode*/
wchar_t* m2w(const char* mbs)
{
int len;
wchar_t* buf;
len =mbstowcs(NULL,mbs,0);
if (len == 0)
return NULL;
buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
memset(buf, 0, sizeof(wchar_t) *(len+1));
len =mbstowcs(buf,mbs,len+1);
return buf;
}
char* ANSIToUTF8(const char* str)
{
return UnicodeToUTF8(ANSIToUnicode(str));
}
char* UTF8ToANSI(const char* str)
{
return UnicodeToANSI(UTF8ToUnicode(str));
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
