C语言 一级注册结构基础考试础

win32中GBK、UTF8的互转
已经经过测试
#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
//GBK编码转换到UTF8编码
int GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen)
wchar_t * lpUnicodeStr = NULL;
int nRetLen = 0;
if(!lpGBKStr)
//如果GBK字符串为NULL则出错退出
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,NULL);
//获取转换到Unicode编码后所需要的字符空间长度
lpUnicodeStr = new WCHAR[nRetLen + 1];
//为Unicode字符串空间
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen);
//转换到Unicode编码
if(!nRetLen)
//转换失败则出错退出
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,NULL);
//获取转换到UTF8编码后所需要的字符空间长度
if(!lpUTF8Str)
//输出缓冲区为空则返回转换后需要的空间大小
if(lpUnicodeStr)
delete []lpUnicodeS
return nRetL
if(nUTF8StrLen < nRetLen)
//如果输出缓冲区长度不够则退出
if(lpUnicodeStr)
delete []lpUnicodeS
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL);
//转换到UTF8编码
if(lpUnicodeStr)
delete []lpUnicodeS
return nRetL
// UTF8编码转换到GBK编码
int UTF8ToGBK(unsigned char * lpUTF8Str,unsigned char * lpGBKStr,int nGBKStrLen)
wchar_t * lpUnicodeStr = NULL;
int nRetLen = 0;
if(!lpUTF8Str)
//如果UTF8字符串为NULL则出错退出
nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,NULL,NULL);
//获取转换到Unicode编码后所需要的字符空间长度
lpUnicodeStr = new WCHAR[nRetLen + 1];
//为Unicode字符串空间
nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,lpUnicodeStr,nRetLen);
//转换到Unicode编码
if(!nRetLen)
//转换失败则出错退出
nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,NULL,NULL,NULL,NULL);
//获取转换到GBK编码后所需要的字符空间长度
if(!lpGBKStr)
//输出缓冲区为空则返回转换后需要的空间大小
if(lpUnicodeStr)
delete []lpUnicodeS
return nRetL
if(nGBKStrLen < nRetLen)
//如果输出缓冲区长度不够则退出
if(lpUnicodeStr)
delete []lpUnicodeS
nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,(char *)lpGBKStr,nRetLen,NULL,NULL);
//转换到GBK编码
if(lpUnicodeStr)
delete []lpUnicodeS
return nRetL
int _tmain(int argc, _TCHAR* argv[])
char cGBKStr[] = "上证指数!";
char * lpGBKStr = NULL;
char * lpUTF8Str = NULL;
FILE * fp = NULL;
int nRetLen = 0;
nRetLen = GBKToUTF8((unsigned char *)cGBKStr,NULL,NULL);
printf("转换后的字符串需要的空间长度为:%d ",nRetLen);
lpUTF8Str = new char[nRetLen + 1];
nRetLen = GBKToUTF8((unsigned char *)cGBKStr,(unsigned char *)lpUTF8Str,nRetLen);
if(nRetLen)
printf("GBKToUTF8转换成功!");
printf("GBKToUTF8转换失败!");
goto Ret0;
fp = fopen("C:\\GBK2UTF8.txt","wb");
//保存到文本文件
fwrite(lpUTF8Str,nRetLen,1,fp);
fclose(fp);
getchar();
//先去打开那个文本文件看看,单击记事本的“文件”-“另存为”菜单,在对话框中看到编码框变为了“UTF-8”说明转换成功了
nRetLen = UTF8ToGBK((unsigned char *)lpUTF8Str,NULL,NULL);
//再转回来
printf("转换后的字符串需要的空间长度为:%d ",nRetLen);
lpGBKStr = new char[nRetLen + 1];
nRetLen = UTF8ToGBK((unsigned char *)lpUTF8Str,(unsigned char *)lpGBKStr,nRetLen);
if(nRetLen)
printf("UTF8ToGBK转换成功! ");
printf("UTF8ToGBK转换失败! ");
goto Ret0;
fp = fopen("C:\\UTF82GBK.txt","wb");
//保存到文本文件
fwrite(lpGBKStr,nRetLen,1,fp);
fclose(fp);
getchar();
//再去打开文本文件看看,发现编码框又变为了“ANSI”说明转换成功了
if(lpGBKStr)
delete []lpGBKS
if(lpUTF8Str)
delete []lpUTF8S
来源:/chuncn/archive//2013895.html
留下你的足迹
All rights reserved.
Images From:推荐这篇日记的豆列
······似乎我们遇到了一些阻力。
(抱歉,找不到您所搜索的页面。)
仍未找到所需的内容?oracle11g更改字符集AL32UTF8为ZHS16GBK_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
oracle11g更改字符集AL32UTF8为ZHS16GBK
上传于|0|0|文档简介
&&oracle11g更改字符集AL32UTF8为ZHS16GBK
你可能喜欢

我要回帖

更多关于 数据结构基础知识 的文章

 

随机推荐