当前位置: 代码迷 >> VC >> Encoding转UTF8,该怎么处理
  详细解决方案

Encoding转UTF8,该怎么处理

热度:5830   发布时间:2013-02-25 00:00:00.0
Encoding转UTF8
求高手给完整的代码,我是一点VC都不会的,麻烦了,被逼着做这个的
帮帮忙吧

------解决方案--------------------------------------------------------
去看MSDN吧...http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx

just example...
C/C++ code
using namespace System;using namespace System::Text;using namespace System::Collections;int main(){   // Create a UTF-8 encoding.   UTF8Encoding^ utf8 = gcnew UTF8Encoding;   // A Unicode string with two characters outside an 8-bit code range.   String^ unicodeString = L"This unicode string contains two characters with codes outside an 8-bit code range, Pi (\u03a0) and Sigma (\u03a3).";   Console::WriteLine( "Original string:" );   Console::WriteLine( unicodeString );   // Encode the string.   array<Byte>^encodedBytes = utf8->GetBytes( unicodeString );   Console::WriteLine();   Console::WriteLine( "Encoded bytes:" );   IEnumerator^ myEnum = encodedBytes->GetEnumerator();   while ( myEnum->MoveNext() )   {      Byte b = safe_cast<Byte>(myEnum->Current);      Console::Write( "[{0}]", b );   }   Console::WriteLine();   // Decode bytes back to string.   // Notice Pi and Sigma characters are still present.   String^ decodedString = utf8->GetString( encodedBytes );   Console::WriteLine();   Console::WriteLine( "Decoded bytes:" );   Console::WriteLine( decodedString );}
------解决方案--------------------------------------------------------

// 文本编码转换
bool Ansi2Unicode(const std::string& ansi, std::wstring& uni)
{
if(ansi.size() == 0)
{
uni.resize(0);
return true;
}

// 预转换,得到所需空间的大小
int wcsLen = MultiByteToWideChar(CP_ACP, NULL, ansi.c_str(), ansi.size(), NULL, 0);

wchar_t* uni_Buff = new wchar_t[wcsLen + 1];
memset(uni_Buff, 0x00, sizeof(wchar_t) * (wcsLen + 1));

// 转换
MultiByteToWideChar(CP_ACP, NULL, ansi.c_str(), ansi.size(), uni_Buff, wcsLen);

uni = uni_Buff;
delete[] uni_Buff;
uni_Buff = NULL;
return true;
}

bool Unicode2Ansi(const std::wstring& unicode, int size, std::string& ansi)
{
if(unicode.size() == 0)
{
ansi.resize(0);
return true;
}

// 预转换,得到所需空间的大小,这次用的函数和上面名字相反
int ansiLen = WideCharToMultiByte(CP_ACP, NULL, unicode.c_str(), size, NULL, 0, NULL, NULL);

char* ansiBuff = new char[ansiLen + 1];
memset(ansiBuff, 0x00, (ansiLen + 1));

//unicode版对应的strlen是wcslen
WideCharToMultiByte(CP_ACP, NULL, unicode.c_str(), size, ansiBuff, ansiLen, NULL, NULL);

ansi = ansiBuff;
delete[] ansiBuff;
ansiBuff = NULL;
return true;
}

bool Utf8ToUnicode(const std::string& utf8, std::wstring& unicode)
{
if (utf8.size() == 0)
{
unicode.resize(0);
return true;
}

//预转换,得到所需空间的大小
int wcsLen = MultiByteToWideChar(CP_UTF8, NULL, utf8.c_str(), utf8.size(), NULL, 0);

wchar_t* unicode_buf = new wchar_t[wcsLen + 1];

memset(unicode_buf, 0x00, sizeof(wchar_t) * (wcsLen + 1));

//转换
MultiByteToWideChar(CP_UTF8, NULL, utf8.c_str(), utf8.size(), unicode_buf, wcsLen);

unicode = unicode_buf;

delete[] unicode_buf;
unicode_buf = NULL;
return true;
}

bool Unicode2Utf8(const std::wstring& unicode, int size, std::string& utf8)
{
if (unicode.size() == 0)
{
utf8.resize(0);
return true;
}

//预转换,得到所需空间的大小,这次用的函数和上面名字相反
int u8Len = WideCharToMultiByte(CP_UTF8, NULL, unicode.c_str(), size, NULL, 0, NULL, NULL);

char* utf8_buff = new char[u8Len + 1];
memset(utf8_buff, 0x00, u8Len + 1);
//转换
//unicode版对应的strlen是wcslen
WideCharToMultiByte(CP_UTF8, NULL, unicode.c_str(), size, utf8_buff, u8Len, NULL, NULL);
  相关解决方案