Windows 下字节转换
#include <string>
#include <windows.h> class CharsConversion
{
public:
static bool MultiByte2UTF8( const std::string&, std::string& ); static bool UTF82MultiByte( const std::string&, std::string& ); static bool MultiByte2Unicode( const std::string&, std::wstring& ); static bool Unicode2MultiByte( const std::wstring&, std::string& ); static bool UTF82Unicode( const std::string&, std::wstring& ); static bool Unicode2UTF8( const std::wstring&, std::string& );
}; bool CharsConversion::MultiByte2UTF8( const std::string &strMB, std::string& strUTF8 )
{
//Note: If this cbMultiByte is –1, the string is assumed to be null terminated and the length is calculated automatically.
//Note: If cbMultiByte is strMB.size(), this function will not include the null terminated, it's an error.
//Note: Or, if cbMultiByte is strMB.size() + 1, this function will be right.
int nWCLen = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, NULL, ); wchar_t* pszWC = new (std::nothrow) wchar_t[nWCLen];
if ( nullptr == pszWC ) return false; //Note: The return value includes the NULL termination character.
int nRtn = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, pszWC, nWCLen );
if( nRtn != nWCLen ) { delete[] pszWC; return false; } // WindChar 2 UTF8
//Note: nWClen and -1 both ok.
int nUTF8Len = WideCharToMultiByte( CP_UTF8, , pszWC, nWCLen, NULL, , NULL, NULL );
if ( nUTF8Len <= ) { delete [] pszWC; return false; } strUTF8.resize( nUTF8Len );
nRtn = WideCharToMultiByte( CP_UTF8, , pszWC, nWCLen, &strUTF8[], nUTF8Len, NULL, NULL );
delete [] pszWC; if ( nRtn != nUTF8Len ) { strUTF8.clear(); return false; } return true;
} bool CharsConversion::UTF82MultiByte( const std::string &strUTF8, std::string &strMB )
{
//Note: cchWideChar must be -1 or strUTF8.size()+1.
int nWCLen = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, NULL, ); WCHAR* pszWC = new (std::nothrow) WCHAR[nWCLen];
if ( nullptr == pszWC ) return false; int nRtn = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, pszWC, nWCLen );
if ( nRtn != nWCLen ) { delete[] pszWC; return false; } //WideChar 2 MB
int nMBLen = WideCharToMultiByte( CP_ACP, , pszWC, nWCLen/*or -1*/, NULL, , NULL, NULL );
if ( nMBLen <= ) { delete [] pszWC; return false; } strMB.resize( nMBLen );
nRtn = WideCharToMultiByte( CP_ACP, , pszWC, nWCLen/*or -1*/, &strMB[], nMBLen, NULL, NULL );
delete [] pszWC; if ( nRtn != nMBLen ) { strMB.clear(); return false; } return true;
} bool CharsConversion::MultiByte2Unicode( const std::string &strMB, std::wstring &strUC )
{
//Note: cbMultiByte can be strMB.size() + 1 or -1.
int nUCLen = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, NULL, ); if ( nUCLen <= ) return false; strUC.resize( nUCLen ); int nRtn = MultiByteToWideChar( CP_ACP, , strMB.c_str(), -, &strUC[], nUCLen ); if ( nRtn != nUCLen ) { strUC.clear(); return false; } return true;
} bool CharsConversion::Unicode2MultiByte( const std::wstring &strUC, std::string &strMB )
{
//Note: cchWideChar must be -1 or strUC.size()+1.
int nMBLen = WideCharToMultiByte( CP_ACP, , strUC.c_str(), -, NULL, , NULL, NULL ); if ( nMBLen <= ) return false; strMB.resize( nMBLen ); int nRtn = WideCharToMultiByte( CP_ACP, , strUC.c_str(), -, &strMB[], nMBLen, NULL, NULL ); if( nRtn != nMBLen ) { strMB.clear(); return false; } return true;
} bool CharsConversion::UTF82Unicode( const std::string &strUTF8, std::wstring &strUC )
{
//Note: cbMultiByte can be strUTF8.size() + 1 or -1.
int nUCLen = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, NULL, ); if ( nUCLen <= ) return false; strUC.resize( nUCLen ); int nRtn = MultiByteToWideChar( CP_UTF8, , strUTF8.c_str(), -, &strUC[], nUCLen ); if ( nRtn != nUCLen ) { strUC.clear(); return false; } return true;
} bool CharsConversion::Unicode2UTF8( const std::wstring &strUC, std::string &strUTF8 )
{
//Note: cchWideChar must be -1 or strUC.size()+1.
int nUTF8Len = WideCharToMultiByte( CP_UTF8, , strUC.c_str(), -, NULL, , NULL, NULL ); if ( nUTF8Len <= ) return false; strUTF8.resize( nUTF8Len ); int nRtn = WideCharToMultiByte( CP_UTF8, , strUC.c_str(), -, &strUTF8[], nUTF8Len, NULL, NULL ); if ( nRtn != nUTF8Len ) { strUTF8.clear(); return false; } return true;
}