VC++版 Unicode ANSI UTF-8 GB2312 相互转换代码

void AnsiToUnicode(char *szAnsi,WCHAR * szWs)
{
//必须保证szWs有足够的空间
int len = ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0);
::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), szWs, len);
szWs[len] = '\0';
}
void UnicodeToAnsi(WCHAR *szWs,char * szAnsi)
{
//保证szAnsi有足够的空间
int len = ::WideCharToMultiByte(CP_ACP, NULL, szWs, wcslen(szWs), NULL, 0, NULL, NULL);
::WideCharToMultiByte(CP_ACP, NULL, szWs, wcslen(szWs), szAnsi, len, NULL, NULL);
szAnsi[len] = '\0';
}
void UnicodeToUTF8(WCHAR * szWs,char* szU8)
{
int len = ::WideCharToMultiByte(CP_UTF8, NULL, szWs, wcslen(szWs), NULL, 0, NULL, NULL);
::WideCharToMultiByte(CP_UTF8, NULL, szWs, wcslen(szWs), szU8, len, NULL, NULL);
szU8[len] = '\0';
}
void UTF8ToUnicode(char * szU8,WCHAR *szWs)
{
int len = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), szWs, len);
szWs[len] = '\0';
}
网络转载转换类,未验证:
class CChineseCode
{
public:
static void UTF_8ToUnicode(wchar_t* pOut,char *pText); // 把UTF-8转换成Unicode
static void UnicodeToUTF_8(char* pOut,wchar_t* pText); //Unicode 转换成UTF-8
static void UnicodeToGB2312(char* pOut,wchar_t uData); // 把Unicode 转换成 GB2312
static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);// GB2312 转换成 Unicode
static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);//GB2312 转为 UTF-8
static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//UTF-8 转为 GB2312
};
类实现
void CChineseCode::UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void CChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void CChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);
return;
}
void CChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return ;
}
void CChineseCode::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
int nLength = pLen* 3;
char* rst = new char[nLength];
memset(buf,0,4);
memset(rst,0,nLength);
int i = 0;
int j = 0;
while(i < pLen)
{
//如果是英文直接复制就可以
if( *(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
wchar_t pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
rst[j] = '';
//返回结果
pOut = rst;
delete []rst;
return;
}
void CChineseCode::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char * newBuf = new char[pLen];
char Ctemp[4];
memset(Ctemp,0,4);
int i =0;
int j = 0;
while(i < pLen)
{
if(pText[i] > 0)
{
newBuf[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0];
newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
newBuf[j] = '';
pOut = newBuf;
delete []newBuf;
return;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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中某些特殊字符或语言特定的字符。因此,在转换编码时,需要确认目标编码是否支持源内容中使用的所有字符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值