UTF8変換する為には、MultiByteToWideChar()関数を使用します。第1引数にCP_UTF8を渡す事でUTF8文字列を扱う事ができます。
#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <string>
#include <iostream>
#include <windows.h>
/*
UTF8文字列をwstringに変換する
*/
std::wstring Utf8ToWString
(
std::string oUTF8Str
)
{
// バッファサイズの取得
int iBufferSize = ::MultiByteToWideChar( CP_UTF8, 0, oUTF8Str.c_str()
, -1, (wchar_t *)NULL, 0 );
// バッファの取得
wchar_t* wpBufWString = (wchar_t*)new wchar_t[ iBufferSize ];
// UTF8 → wstring
::MultiByteToWideChar( CP_UTF8, 0, oUTF8Str.c_str(), -1, wpBufWString
, iBufferSize );
// wstringの生成
std::wstring oRet( wpBufWString, wpBufWString + iBufferSize - 1 );
// バッファの破棄
delete [] wpBufWString;
// 変換結果を返す
return( oRet );
}
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
// 標準出力にユニコード出力する
setlocale( LC_ALL, "Japanese" );
// UTF8文字列をwstringに変換する
std::wstring oWString = Utf8ToWString( "Unicode UTF8 String" );
// 標準出力へ出力する
std::wcout << oWString << std::endl;
// 正常終了
return( 0 );
}
Unicode UTF8 String