关于UTF-8、GB2313、ANSI、UNICODE的
编码问题,在此不多说,百度上资料很多的。
以下为源代码:
class="c++">
// UtfFile.h: interface for the UtfFile class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_UTFFILE_H__8ED10D8A_D1A3_412F_A600_124F521CE4F1__INCLUDED_)
#define AFX_UTFFILE_H__8ED10D8A_D1A3_412F_A600_124F521CE4F1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
#include <stdio.h>
#include <locale.h>
#include <IOSTREAM>
#include <FSTREAM>
using namespace std;
class UtfFile
{
public:
static char * UnicodeToGB2312(WCHAR uData, char buffer[2]); // Unicode 转换成 GB2312
static WCHAR * UTF_8ToUnicode(char *pText, WCHAR &unicode); // 把UTF-8 转化成 Unicode
static char * TranslateUTF8ToGB(char *str, size_t len); // 把UTF-8字符串转化成ANSI(GB2312)编码形式
char * GetString(char *str, int maxLen = 1024); // 读取一个字符串,以换行符为结束标示
char * GetLine(char *str, int maxLen); // 读取一行字符
void close(); // 关闭文件流
int open(const char *sFileName); // 用于打开一个文件
UtfFile(const char *sour);
virtual ~UtfFile();
private:
ifstream inf;
};
#endif // !defined(AFX_UTFFILE_H__8ED10D8A_D1A3_412F_A600_124F521CE4F1__INCLUDED_)
/*-----------------------------------------------以下为.cpp文件内容------------------------------------------*/
// UtfFile.cpp: implementation of the UtfFile class.
//
//////////////////////////////////////////////////////////////////////
#include "UtfFile.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
UtfFile::UtfFile(const char *sour)
:inf(sour)
{
if( !inf.is_open() )
{
char str[1024];
sprintf(str, "错误的文件路径,文件无法打开:%s", sour);
throw runtime_error(str);
}
}
UtfFile::~UtfFile()
{
}
void UtfFile::close()
{
inf.close();
}
char * UtfFile::GetLine(char *str, int maxLen)
{
if ( inf.eof() )
{
str[0] = '\0';
}
else
{
inf.getline(str, maxLen);
TranslateUTF8ToGB(str, maxLen);
}
return str;
}
char * UtfFile::GetString(char *str, int maxLen)
{
if ( inf.eof() )
{
str[0] = '\0';
}
else
{
inf >> str;
TranslateUTF8ToGB(str, maxLen);
}
return str;
}
char * UtfFile::UnicodeToGB2312(WCHAR uData, char buffer[2])
{
WideCharToMultiByte(CP_ACP,NULL, &uData, 1,buffer,sizeof(WCHAR),NULL,NULL);
return buffer;
}
WCHAR * UtfFile::UTF_8ToUnicode(char *pText, WCHAR &unicode)
{
/* http://blog.csdn.net/liuzhiyuan1982/article/details/3911150
UTF-8是一种多字节编码的字符集,表示一个Unicode字符时,它可以是1个至多个字节,在表示上有规律:
1字节:0xxxxxxx
2字节:110xxxxx 10xxxxxx
3字节:1110xxxx 10xxxxxx 10xxxxxx
4字节:11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
char *uchar = (char *)&unicode;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return &unicode;
}
char * UtfFile::TranslateUTF8ToGB(char *str, size_t len)
{
char * newCharBuffer = new char[len];
int index =0;
int nCBIndex = 0;
WCHAR wTemp = 0;
char cTemp[2] = " ";
while(index < len)
{
if ( str[index] == 0 )
break;
else if(str[index] > 0) // 如果是GB2312的字符
{
newCharBuffer[nCBIndex] = str[index]; //直接复制
index += 1; //源字符串偏移量1
nCBIndex += 1; //目标字符串偏移量1
}
else //如果是UTF-8的字符
{
UTF_8ToUnicode(str + index, wTemp); //先把UTF-8转成Unicode
UnicodeToGB2312(wTemp, &newCharBuffer[nCBIndex]); //再把Unicode 转成 GB2312
index += 3; //源字符串偏移量3
nCBIndex += 2; //目标字符串偏移量2 因为一个中文UTF-8占3个字节,GB2312占两个字节
}
}
newCharBuffer[nCBIndex] = '\0'; //结束符
strcpy( str, newCharBuffer );
delete newCharBuffer; //避免内存泄漏,这是对源代码的稍许修改
newCharBuffer = NULL;
return str;
}
int UtfFile::open(const char *sFileName)
{
inf.open(sFileName);
return inf.is_open();
}