python编码转换工具_编码转换工具 源码

//Change.cpp: implementation of the CChange class.

#include"stdafx.h"#include"CodeChange.h"#include"Change.h"#ifdef _DEBUG#undef THIS_FILE

static char THIS_FILE[]=__FILE__;#define new DEBUG_NEW

#endif

//

//Construction/Destruction

//

CChange::CChange()

{

}

CChange::~CChange()

{

}

CString CChange::Encode_ansitounicode(CString str_temp)

{

CString return_temp;//声明返回变量

CString szAnsi = str_temp; //要进行转换编码的字符串

int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0); //获得所需空间大小

wchar_t * wszString = new wchar_t[wcsLen+1]; //声明wchar_t变量,用于保存转换后的值

::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen); //进行转换

wszString[wcsLen] = '\0'; //加上结束符

char *buf = (char *)malloc(wcsLen*2); //分配空间到变量

buf =(char *)wszString; //定义变量//AfxMessageBox(buf);

for(int i=0;i

{

str_temp.Format("%2.2x", buf[i]); //转成十六进制格式

str_temp = str_temp.Right(2); //取右二位

return_temp = return_temp + str_temp + " "; //用空格隔开//AfxMessageBox(return_temp);

}//#ifdef _DEBUG

CFile cFile;

cFile.Open(_T("ansitounicode.txt"), CFile::modeWrite |CFile::modeCreate);//文件开头

cFile.SeekToBegin();//写入内容

cFile.Write("\xff\xfe", 2);

cFile.Write(wszString, wcsLen* sizeof(wchar_t));

cFile.Flush();

cFile.Close();//#endif

delete[] wszString;

wszString=NULL;returnreturn_temp;

}

CString CChange::Encode_unicodetoansi(CString str_temp)

{

CString return_temp;//声明返回变量

char *endptr; //暂末用到

char string1[2], string2[2]; //用于存放两个字节

char *str_char = str_temp.GetBuffer(str_temp.GetLength()); //声明一个指针,指向字符串所映射的内存//if(str_temp.GetLength() == 0 || 0 != str_temp.GetLength()%4)//简单的异常处理//{//AfxMessageBox("You string have lost some letter! Please input again!");//return "";//}

wchar_t* wszString = new wchar_t[str_temp.GetLength()/4+1]; //声明wchar_t变量(unsigned short)

for(int i = 0;i < str_temp.GetLength()/4;i++) //对两个两个数据进行处理

{//memcpy(string1, str_char+4*i, 2);//将str_char中的数据的2个字节复制到string1//memcpy(string2, str_char+2+4*i, 2);

string1[0] = str_temp[i*4];

string1[1] = str_temp[i*4+1];

string2[0] = str_temp[i*4+2];

string2[1] = str_temp[i*4+3];

CString string3;

string3.Format("%c%c%c%c", string2[0],string2[1],string1[0],string1[1]);//memcpy(string3, string2, 2);//memcpy(string3+2, string1, 2);//复制4个字节数据到string3

long num = strtol(string3, &endptr, 16);

wszString[i]= num; //转成16进制存入数组

}

wszString[str_temp.GetLength()/4] = '\0'; //插入结束符

int ansiLen = str_temp.GetLength()/4; //目录字符数据大小

for(int j=0; j < str_temp.GetLength()/4; j++)

{if(wszString[j]>256)

ansiLen++;

}//int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);

char* szAnsi = new char[ansiLen + 1]; //声明保存数据变量

::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL); //进行转换

szAnsi[ansiLen] = '\0'; //赋结束符

return_temp=szAnsi;//#ifdef _DEBUG

CFile cFile;

cFile.Open(_T("unicodetoansi.txt"), CFile::modeWrite |CFile::modeCreate);//文件开头

cFile.SeekToBegin();//写入内容

cFile.Write(szAnsi, ansiLen * sizeof(char));

cFile.Flush();

cFile.Close();//#endif

delete[] szAnsi;

szAnsi=NULL;returnreturn_temp;

}

CString CChange::Encode_ansitoutf8(CString str_temp)

{

CString return_temp;//声明返回变量

CString szAnsi = str_temp; //要进行转换编码的字符串

int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0); //获得所需空间大小

wchar_t * wszString = new wchar_t[wcsLen+1]; //声明wchar_t变量,用于保存转换后的值

::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen); //进行转换

wszString[wcsLen] = '\0'; //加上结束符//到此时wszString里已经是转成unicode编码的数据了,接下来就只要将unicode编成utf-8就可以了//wchar_t* wszString = L"中国a";//测试用的数据

int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString),NULL, 0,NULL,NULL); //所需数据大小

char* szU8 = new char[u8Len+1]; //保存utf-8编码数据//UTF8虽然是Unicode的压缩形式,但也是多字节字符串,所以可以以char的形式保存//unicode版对应的strlen是wcslen

::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL); //进行 utf-8转码

szU8[u8Len] = '\0'; //加上结束符//MessageBox不支持UTF8,所以只能写文件//return_temp = szU8;//将数据赋给 返回值变量

for(int i=0;i

{

str_temp.Format("%2.2x", szU8[i]); //转成十六进制格式

str_temp = str_temp.Right(2); //取右二位

return_temp = return_temp + str_temp + " "; //用空格隔开//AfxMessageBox(return_temp);

}//#ifdef _DEBUG//写文本文件,UTF8的BOM是0xbfbbef

CFile cFile;

cFile.Open(_T("ansitoutf8.txt"), CFile::modeWrite |CFile::modeCreate);

cFile.SeekToBegin();

cFile.Write("\xef\xbb\xbf", 3);//写BOM,同样低位写在前

cFile.Write(szU8, u8Len*sizeof(char));

cFile.Flush();

cFile.Close();//#endif

delete[] szU8;

szU8=NULL;returnreturn_temp;

}

CString CChange::Encode_utf8toansi(CString str_temp)

{

CString return_temp;//声明返回变量

char *endptr; //暂末用到//char* szU8 = "abcd1234\xe4\xbd\xa0\xe6\x88\x91\xe4\xbb\x96\x00";//测试数据

char *szU8 = new char[str_temp.GetLength()/2+1]; //存放要转换的数据

char string1[1]; //用于存放两个字节

char * str_char = str_temp.GetBuffer(str_temp.GetLength()); //声明一个指针,指向字符串所映射的内存

str_temp.ReleaseBuffer(); //释放对象

for(int i = 0;i < str_temp.GetLength()/2;i++) //两位两位的处理,将字符串转成十六进制存储

{

memcpy(string1, str_char+i*2, 2);

szU8[i]= (char)strtol(string1, &endptr, 16);

}

szU8[str_temp.GetLength()/2] = '\0'; //加入结束符

int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8),NULL, 0); //获取常量大小

wchar_t* wszString = new wchar_t[wcsLen+1]; //保存数据的变量

::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen); //多位转字节

wszString[wcsLen] = '\0'; //加入结束符

int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); //获取常量大小

char* szAnsi = new char[ansiLen+1]; //声明保存数据变量

::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL); //进行转换

szAnsi[ansiLen] = '\0'; //赋结束符

return_temp =szAnsi;//#ifdef _DEBUG

CFile cFile;

cFile.Open(_T("utf8toansi.txt"), CFile::modeWrite |CFile::modeCreate);//文件开头

cFile.SeekToBegin();//写入内容

cFile.Write(szAnsi, ansiLen * sizeof(char));

cFile.Flush();

cFile.Close();//#endif

delete[] szAnsi;

delete[] szU8;

szU8=NULL;

szAnsi=NULL;//AfxMessageBox(return_temp);

returnreturn_temp;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值