欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ANSI和UTF8格式文件的互相转换

程序员文章站 2022-03-31 10:30:09
...

ANSI和UTF8格式文件的互相转换

本文描述如何在UTF8和ANSI之间相互转换。
转换关键点1:
UTF8文件有独特的文件头标志,前面3个字节为:0xEF, 0xBB, 0xBF;
ANSI文件没有头文件,直接开始放置内容。
转换关键点2:
UTF8和ANSI相互转换时,均需要使用UNICODE作为中间转换值;也就是UTF8先转为UNICODE,然后再将UNICODE转换为ANSI;反之亦然。
以下代码可以将ANSI编码格式的文件转换为UTF8编码的文件:

#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;
}

以下代码可以将UTF8编码格式的文件转换为ANSI编码的文件:

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;
}