Original link: https://blog.csdn.net/qq_23313467/article/details/104842156
Mutual conversion between ANSI and UTF8 format files
This article describes how to convert between UTF8 and ANSI.
Conversion key point 1:
UTF8 files have a unique file header flag, the first 3 bytes are: 0xEF, 0xBB, 0xBF;
ANSI files do not have a header file and start placing content directly.
Conversion key point 2:
When UTF8 and ANSI are converted to each other, UNICODE is required as the intermediate conversion value; that is, UTF8 is converted to UNICODE first, and then UNICODE is converted to ANSI; and vice versa.
The following code can convert ANSI-encoded files to UTF8-encoded files:
#include <iostream>
#include <direct.h>
#include <string>
#include <io.h>
#include <stdio.h>
#include <windows.h>
using namespace std;
int FileAnsi2UTF8(char* strFileName)
{
//CopyFile(strFileName,("newUtf8.txt"),FALSE);//
FILE *fp = fopen(strFileName, "r");//FILE *fp = fopen("cc.cfg", "r+");
if(fp == NULL)
{
cout << "open error";//printf("open error");
return -1;
}
byte head[3];
fread(head,3,1,fp);//fileReader.Read(head, 3);
//判断是否带有BOM文件头
if (head[0] == 0xef && head[1] == 0xbb && head[2] == 0xbf)
{
fclose(fp);//fileReader.Close();
return -30;
}
fseek(fp,0,SEEK_SET);fileReader.SeekToBegin();
char linebuffer[512] = {0};
char linenew[1024]={0};
FILE *fpnew = fopen("newUtf8.txt", "w+");//FILE *fp = fopen("cc.cfg", "r+");
if(fpnew == NULL)
{
cout << "open new error";//printf("open new error");
fclose(fp);
return -1;
}
const unsigned char aryBOM[] = { 0xEF, 0xBB, 0xBF };
fwrite(aryBOM, sizeof(aryBOM),1,fpnew);
while(fgets(linebuffer, 512, fp))
{
int len = MultiByteToWideChar(CP_ACP, 0, linebuffer, -1, NULL, 0);//strlen(gb2312);//
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, linebuffer, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr) delete[] wstr;
str[len] = 0;//'\n';
fwrite(str, len-1,1,fpnew);//fp.Write(str, len);
delete[] str;
}
fclose(fp);
fclose(fpnew);
return 0;
}
The following code can convert a UTF8-encoded file to an ANSI-encoded file:
int FileUtf82ANSI(char* strFileName)
{
FILE *fp = fopen(strFileName, "r");//FILE *fp = fopen("cc.cfg", "r+");
if(fp == NULL)
{
cout << "open error";//printf("open error");
return -1;
}
byte head[3];
fread(head,3,1,fp);//fileReader.Read(head, 3);
//判断是否带有BOM文件头
if (head[0] != 0xef || head[1] != 0xbb || head[2] != 0xbf)
{
fclose(fp);//fileReader.Close();
return -30;
}
fseek(fp,3,SEEK_SET);fileReader.SeekToBegin();
char linebuffer[512] = {0};
char linenew[1024]={0};
FILE *fpnew = fopen("newAnsi.txt", "w+");//FILE *fp = fopen("cc.cfg", "r+");
if(fpnew == NULL)
{
cout << "open new error";//printf("open new error");
fclose(fp);
return -1;
}
//const unsigned char aryBOM[] = { 0xEF, 0xBB, 0xBF };
//fwrite(aryBOM, sizeof(aryBOM),1,fpnew);
while(fgets(linebuffer, 512, fp))
{
//预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, linebuffer, -1, NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t * pUcs2 = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_UTF8, NULL, linebuffer, -1, pUcs2, wcsLen);
//最后加上'\0'
pUcs2[wcsLen] = '\0';
//预转换,得到所需空间的大小,这次用的函数和上面名字相反
int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, pUcs2, wcslen(pUcs2), NULL, 0, NULL, NULL);
//同上,分配空间要给'\0'留个空间
char * pAnsi = new char[ansiLen + 1];
//转换
//unicode版对应的strlen是wcslen
::WideCharToMultiByte(CP_ACP, NULL, pUcs2, wcslen(pUcs2), pAnsi, ansiLen, NULL, NULL);
//最后加上'\0'
pAnsi[ansiLen] = '\0';
if (pUcs2) delete[] pUcs2;
fwrite(pAnsi, ansiLen,1,fpnew);//fp.Write(str, len);
delete[] pAnsi;
}
fclose(fp);
fclose(fpnew);
return 0;
}
————————————————