shift-jis 转 utf-8 c++

时间:2023-01-06 11:53:34
#include "stdafx.h"
#include "shift_jisTo_utf_8.h"
#include <iostream>
#include <fstream>
using namespace std;
wchar_t* sjisToUnicode(char* src);
char* unicodeToUtf8(wchar_t* wText);
void main(){
	char *srcText = new char;

	fstream f("D:\\a.txt", ios::in | ios::binary);
	f >> srcText;
	f.close();

	wchar_t *tmp = sjisToUnicode(srcText);
	char *dstText = unicodeToUtf8(tmp);

	f.open("D:\\b.txt", ios::out | ios::binary);
	f << dstText;
	f.close();
}
wchar_t* sjisToUnicode(char* src)
{
	DWORD dwNum = MultiByteToWideChar(932, 0, src, -1, NULL, 0);
	wchar_t *dst = new wchar_t[dwNum];
	if (!dst)
	{
		delete[]dst;
	}
	MultiByteToWideChar(932, 0, src, -1, dst, dwNum);
	return dst;
}
char* unicodeToUtf8(wchar_t* wText)
{
	DWORD dwNum = WideCharToMultiByte(65001, NULL, wText, -1, NULL, 0, NULL, FALSE);
	char *psText;
	psText = new char[dwNum];
	if (!psText)
	{
		delete[]psText;
	}
	WideCharToMultiByte(
		65001
		, NULL, wText, -1, psText, dwNum, NULL, FALSE);
	return psText;
}
shift-jis 转 utf-8 c++