// Change.cpp: implementation of the CChange class.////#include "stdafx.h"#include "CodeChange.h"#include "Change.h"#ifdef _DEBUG#undef THIS_FILEstatic 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.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; return return_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.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); return return_temp;}