The conversion method for Unicode, Utf-8, gb2312 Three encoding formats using vc++6.0 in the Windows Development environment:
[CPP]View Plaincopy
- #include <iostream>
- #include <string>
- #include <Windows.h>
- Using namespace std;
- void UnicodeToUTF8 (const wstring &SRC, string& result)
- {
- int n = widechartomultibyte (Cp_utf8, 0, Src.c_str (),-1, 0, 0, 0, 0);
- Result.resize (n);
- :: WideCharToMultiByte (Cp_utf8, 0, Src.c_str (),-1, (char*) result.c_str (), Result.length (), 0, 0);
- }
- void unicodeToGB2312 (const wstring& WSTR, string& result)
- {
- int n = widechartomultibyte (CP_ACP, 0, Wstr.c_str (),-1, 0, 0, 0, 0);
- Result.resize (n);
- :: WideCharToMultiByte (CP_ACP, 0, Wstr.c_str (),-1, (char*) result.c_str (), n, 0, 0);
- }
- void Utf8tounicode (const string& SRC, wstring& result)
- {
- int n = MultiByteToWideChar (Cp_utf8, 0, Src.c_str (),-1, NULL, 0);
- Result.resize (n);
- :: MultiByteToWideChar (Cp_utf8, 0, Src.c_str (),-1, (lpwstr) result.c_str (), result.length ());
- }
- void Gb2312tounicode (const string& SRC, wstring& result)
- {
- int n = MultiByteToWideChar (CP_ACP, 0, Src.c_str (),-1, NULL, 0);
- Result.resize (n);
- :: MultiByteToWideChar (CP_ACP, 0, Src.c_str (),-1, (lpwstr) result.c_str (), result.length ());
- }
- void Printbyte (String str)
- {
- int i=0;
- For (i=0; i<str.length (); i++)
- {
- printf ("%x", (unsigned char) str.at (i));
- }
- printf ("\ n");
- }
- void Wprintbyte (wstring str)
- {
- int i=0;
- For (i=0; I<str.length () *sizeof (wchar_t); i++)
- {
- printf ("%x", * ((unsigned char*) str.c_str () +i));
- }
- printf ("\ n");
- }
- int main ()
- {
- String strText = "ab kanji";
- String strUTF8;
- Wstring Wstrunicode;
- String strGB2312;
- Gb2312tounicode (StrText, Wstrunicode);
- printf ("unicode=");
- Wprintbyte (Wstrunicode);
- UnicodeToUTF8 (Wstrunicode, strUTF8);
- printf ("UTF-8 =");
- Printbyte (STRUTF8);
- Utf8tounicode (Strutf8,wstrunicode);
- printf ("unicode=");
- Wprintbyte (Wstrunicode);
- unicodeToGB2312 (wstrunicode,strgb2312);
- printf ("GB2312 =");
- Printbyte (strGB2312);
- return 0;
- }
This is a string of "ab Kanji" for testing, and its ASCII code is a BA ba D7 D6
Output Result: