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

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;
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
UTF-8是一种可变长度的Unicode编码方式,而ANSI是一种字符编码方式。要将UTF-8格式转换ANSI格式,需要使用特定的工具或编程语言来实现。 在Python中,可以使用编码函数来实现UTF-8转换ANSI。首先,我们需要读取以UTF-8编码的文件或字符串,并指定编码为'utf-8'。然后,我们将其使用编码函数进行转换并指定目标编码为'ansi'。 以下是一个示例代码: ```python # 导入codecs模块 import codecs # 以UTF-8编码读取文件内容 with codecs.open('input.txt', 'r', 'utf-8') as file: utf8_content = file.read() # 将UTF-8转换ANSI编码 ansi_content = utf8_content.encode('ansi') # 将ANSI内容写入新文件 with codecs.open('output.txt', 'w', 'ansi') as file: file.write(ansi_content) ``` 在上面的示例中,我们首先使用'codecs.open'函数以'utf-8'编码读取名为'input.txt'的文件内容,并将其存储在变量'utf8_content'中。然后,我们使用编码函数'encode'将'utf8_content'转换ANSI编码的内容,并将结果存储在变量'ansi_content'中。最后,我们将'ansi_content'使用'codecs.open'函数以'ansi'编码写入名为'output.txt'的新文件中。 请注意,在进行编码转换时,可能会出现字符无法完全转换或出现乱码的情况。这是因为ANSI编码方式可能无法表示UTF-8中某些特殊字符或语言特定的字符。因此,在转换编码时,需要确认目标编码是否支持源内容中使用的所有字符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值