Last active
July 2, 2016 19:25
-
-
Save nekko1119/8e102847078cbf3bbff58149437b7f5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <vector> | |
#include <string> | |
BOOL utf8_to_sjis(BYTE* pSource, BYTE* pDist, int* pSize) | |
{ | |
*pSize = 0; | |
//UTF-8 -> UTF-16 | |
const int nSize = ::MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) pSource, -1, NULL, 0); | |
BYTE* buffUtf16 = new BYTE[nSize * 2 + 2]; | |
::MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) pSource, -1, (LPWSTR) buffUtf16, nSize); | |
//UTF-16 -> Shift-JIS | |
const int nSizeSJis = ::WideCharToMultiByte(CP_ACP, 0, (LPCWSTR) buffUtf16, -1, NULL, 0, NULL, NULL); | |
if (!pDist) { | |
*pSize = nSizeSJis; | |
delete buffUtf16; | |
return TRUE; | |
} | |
BYTE* buffSJis = new BYTE[nSizeSJis * 2]; | |
ZeroMemory(buffSJis, nSizeSJis * 2); | |
::WideCharToMultiByte(CP_ACP, 0, (LPCWSTR) buffUtf16, -1, (LPSTR) buffSJis, nSizeSJis, NULL, NULL); | |
*pSize = lstrlen((char*) buffSJis); | |
memcpy(pDist, buffSJis, *pSize); | |
delete buffUtf16; | |
delete buffSJis; | |
return TRUE; | |
} | |
std::string utf8_to_sjis(std::string const& utf8) | |
{ | |
int n; | |
std::vector<unsigned char> sjis(utf8.size(), '\0'); | |
utf8_to_sjis((BYTE*) utf8.data(), sjis.data(), &n); | |
return {sjis.begin(), sjis.end()}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment