Read the file into a buffer, convert the text to Unicode using
MultiByteToWideChar()
, convert it to SHIFT-JIS using
WideCharToMultiByte()
, and write that to the JIS file:
[EDIT: Added code to read and write files]
#include <io.h>
void Utf8FileToJis(LPCTSTR lpszUtf8File, LPCTSTR lpszJisFile)
{
FILE *fUtf8File = _tfopen(lpszUtf8File, _T("rb"));
if (NULL == fUtf8File)
return;
FILE *fJisFile = _tfopen(lpszJisFile, _T("wb"));
if (NULL == fJisFile)
{
fclose(fUtf8File);
return;
}
int nLen = _filelength(fileno(fUtf8File));
LPSTR lpszBuf = new char[nLen];
fread(lpszBuf, 1, nLen, fUtf8File);
int nWideLen = ::MultiByteToWideChar(CP_UTF8, 0, lpszBuf, nLen, NULL, 0);
LPWSTR lpszWide = new WCHAR[nWideLen];
::MultiByteToWideChar(CP_UTF8, 0, lpszBuf, nLen, lpszWide, nWideLen);
int nJisLen = ::WideCharToMultiByte(932, 0, lpszWide, nWideLen, NULL, 0, NULL, NULL);
LPSTR lpszJis = new char[nJisLen];
::WideCharToMultiByte(932, 0, lpszWide, nWideLen, lpszJis, nJisLen, NULL, NULL);
fwrite(lpszJis, 1 , nJisLen, fJisFile);
delete [] lpszJis;
delete [] lpszWide;
delete [] lpszBuf;
fclose(fJisFile);
fclose(fUtf8File);
}