int Unicode2UTF8(const wchar_t* pUnicode
, char* pUTF8Buffer
, int nBufferSize
)
{
if( (nBufferSize
== 0) && (pUTF8Buffer
!= NULL) )
return 0;
int result
= WideCharToMultiByte(CP_UTF8
, NULL, pUnicode
, -1, pUTF8Buffer
, nBufferSize
, NULL, NULL);
if ((result
> 0) && (pUTF8Buffer
!= NULL))
pUTF8Buffer
[result
-1] = 0;
return result
;
}
int UTF82Unicode(const char* pUTF8
, wchar_t* pUnicodeBuffer
, int nBufferSize
)
{
if( (nBufferSize
== 0) && (pUnicodeBuffer
!= NULL) )
return 0;
int result
= MultiByteToWideChar(CP_UTF8
, NULL, pUTF8
, -1, pUnicodeBuffer
, nBufferSize
);
if ((result
> 0) && (pUnicodeBuffer
!= NULL))
pUnicodeBuffer
[result
-1] = 0;
return result
;
}
int Unicode2Ansi(const wchar_t* pUnicode
, char* pAnsiBuffer
, int nBufferSize
)
{
if( (nBufferSize
== 0) && (pAnsiBuffer
!= NULL) )
return 0;
int result
= ::WideCharToMultiByte(CP_ACP
, 0, pUnicode
, -1, pAnsiBuffer
, nBufferSize
, NULL, NULL);
if ((result
> 0) && (pAnsiBuffer
!= NULL))
pAnsiBuffer
[result
-1] = 0;
return result
;
}
int Ansi2Unicode(const char* pAnsi
, wchar_t* pUnicodeBuffer
, int nBufferSize
)
{
if( (nBufferSize
== 0) && (pUnicodeBuffer
!= NULL) )
return 0;
int result
= ::MultiByteToWideChar(CP_ACP
, 0, pAnsi
, -1, pUnicodeBuffer
, nBufferSize
);
if ((result
> 0) && (pUnicodeBuffer
!= NULL))
pUnicodeBuffer
[result
-1] = 0;
return result
;
}
简单实用
CString strBuf
;
int nSize
= Unicode2UTF8(strBuf
, NULL, 0);
char* szBuf
= new char[nSize
];
Unicode2UTF8(strBuf
, szBuf
, nSize
);
转载请注明原文地址:https://tech.qufami.com/read-6012.html