不同編碼字符串的轉換測試
程序代码:
#include "stdafx.h" #include <cstdio> using namespace System; using namespace System::Text; int main(array<String^>^ args) { Console::WriteLine(L"用C的printf()函數輸出不同編碼的字符串測試"); Console::WriteLine(); String^ unicodeString = "bccn論壇歡迎您!"; Encoding^ unicode = Encoding::Unicode; array<Byte>^ unicodeBytes = unicode->GetBytes(unicodeString); Console::WriteLine(L"{0}[{1}]:", unicode->BodyName, unicode->CodePage); //Console::WriteLine(L"{0}", unicode->GetString(unicodeBytes)); printf_s("%s\n", unicodeBytes); for each (Byte b in unicodeBytes) { Console::Write(L"{0} ", b); } Console::WriteLine(); Console::WriteLine(); Encoding^ ansi = Encoding::Default; array<Byte>^ cString = Encoding::Convert(unicode, ansi, unicodeBytes); Console::WriteLine(L"{0}[{1}]:", ansi->BodyName, ansi->CodePage); printf_s("%s\n", cString); for each (Byte b in cString) { Console::Write(L"{0} ", b); } Console::WriteLine(); Console::WriteLine(); Encoding^ utf8 = Encoding::UTF8; array<Byte>^ utf8Bytes = Encoding::Convert(unicode, utf8, unicodeBytes); Console::WriteLine(L"{0}[{1}]:", utf8->BodyName, utf8->CodePage); //Console::WriteLine(L"{0}", utf8->GetString(utf8Bytes)); printf_s("%s\n", utf8Bytes); for each (Byte b in utf8Bytes) { Console::Write(L"{0} ", b); } Console::WriteLine(); Console::WriteLine(); return 0; }