编码windows下ANSI、Unicode、UTF-8编码转换代码

unicode转换  时间:2021-04-12  阅读:()

window s下ANSI 、Unicod e、UTF-8编码转换1几种编码方式的说明

1ANSI 一般的txt编码都是AN SI 可以通过另存为指定其它编码方式在c/c++中的c har 、 unsign ed char等均属于该类型

2Unicod e 分为UTF-8、UTF-16和UTF-32一般在Win dows里面提到Unicode编码方式默认指的是UTF-16

1UTF-8使用1-4字节来表示字符常用的ASC II字符使用1字节汉字一般都是3字节

2UTF-16统一使用2字节表示所有字符不考虑所谓的代理 现在广泛的应用于Windows系统

3UTF-32统一使用4字节表示所有字符。

2转换前准备string、wstrin g #includ e<string>c out、 wc o utcha r、 wc ha r_t

使用wcou t输出wst ring时需要加上setlocale(LC_ALL, ".936");

设置输出窗口的编码方式否则无法输出 。

3编码转换

采用Wind ows提供的API接口函数MultiByte To Wide Char和

MultiByteTo Wi deChar来实现便慢慢转换程序如下

1  ANSI和U nicode的相互转换Window s下Unic ode指的是UTF-16void string2wstring(const string&_str,wstring&_wstr)

{if(_str=="")r e turn;

_wstr=L"";int wlen=MultiByteToWide Char(CP_ACP,0,_str.c_str(), -1,NULL,0);wchar_t*pwc=new wchar_t[wlen];mems et( (char*)pwc,0,wlen*2);

MultiByte ToWide Char(CP_ACP,0,_str.c_str(), -1,pwc,wlen);

_ws tr=pwc;

/*unsigned char*p=(unsigned char*)pwc;for( int i=0; i<wlen*2;++i)c out<<uppercase<<hex<<int(p[i])<<" ";cout<<endl;*/delete[]pwc;

}

2 Unicod e转向ANS I

string&wstring2 string(c onst wstring&_wstr, string&_str)

{if(_wstr==L"")re turn_str;

int len=Wide CharToMultiByte(CP_ACP,0,_wstr.c_str(), -1,NULL,0,NULL,NULL);unsigned char*pc=new unsigned char[len];memset(pc,0, len);

Wi de CharTo MultiByte(CP_ACP,0,_wstr.c_str(), -1, (char*)pc, len,NULL,NULL);

_str=(c har*)pc;

/*for( int i=0; i<len;++i )cout<<uppercase<<hex<<int(pc[i])<<" ";cout<<endl;*/delete[]pc;re turn_str;

}

3 utf-8转向uni code注意utf-8文本的开头会有0xEF、 0xBB和0xBF作为标记void UTF8 string2wstring(const string&_str,wstring&_wstr)

{if(_str=="")r e turn;string str;if(_str.size()>3&&

(uns i gne d c har)_s tr[0]==0 xEF&&

(unsigned char)_str[1]==0xBB&&

(unsigned char)_str[2]==0xBF)str=_str.substr(3);elsestr=_str;

_wstr=L"";int len=_str.size()+1;int wlen=MultiByteToWide Char(CP_UTF8,0, str.c_str(), -1,NULL,0);wchar_t*pwc=new wchar_t[wlen];mems et( (char*)pwc,0,wlen*2);

MultiByte ToWide Char(CP_UTF 8,0,str.c_str(), -1,pwc,wlen);

_ws tr=pwc;

/*unsigned char*p=(unsigned char*)pwc;for( int i=0; i<wlen*2;++i)

c out<<uppercase<<hex<<int(p[i])<<" ";cout<<endl;

*/delete[]pwc;

}

4Unico d e转向UTF-8Unico d e文本的开头是0xFF和0xFEstring&wstring2utf8string(c onst wstring&_wstr, string&_str)

{if(_wstr==L"")re turn_str;

int len=WideCharToMultiByte(CP_UTF8,0,_wstr.c_str(), -1,NULL,0,NULL,NULL);unsigned char*pc=new unsigned char[len];memset(pc,0, len);

Wide CharToMultiByte(CP_UTF 8,0,_wstr.c_str(), -1, (char*)pc, len,NULL,NULL);

_str=(c har*)pc;

/*for( int i=0; i<len;++i )cout<<uppercase<<hex<<int(pc[i])<<" ";cout<<endl;*/delete[]pc;re turn_str;

}

RackNerd提供四款高配美国服务器促销活动低至月$189

RackNerd 商家给的感觉就是一直蹭节日热点,然后时不时通过修改配置结构不断的提供低价年付的VPS主机,不过他们家还是在做事的,这么两年多的发展,居然已经有新增至十几个数据中心,而且产品线发展也是比较丰富。比如也有独立服务器业务,不过在他们轮番的低价年付VPS主机活动下,他们的服务器估摸着销路不是太好的。这里,今天有看到RackNerd商家的独立服务器业务有促销。这次提供美国多个机房的高配独立...

妮妮云,美国cera CN2线路,VPS享3折优惠

近期联通CUVIP的线路(AS4837线路)非常火热,妮妮云也推出了这类线路的套餐以及优惠,目前到国内优质线路排行大致如下:电信CN2 GIA>联通AS9929>联通AS4837>电信CN2 GT>普通线路,AS4837线路比起前两的优势就是带宽比较大,相对便宜一些,所以大家才能看到这个线路的带宽都非常高。妮妮云互联目前云服务器开放抽奖活动,每天开通前10台享3折优惠,另外...

Hostinger 限时外贸美国主机活动 低至月12元且赠送1个COM域名

Hostinger 商家我们可能一些新用户不是太熟悉,因为我们很多新人用户都可能较多的直接从云服务器、独立服务器起步的。而Hostinger商家已经有将近十年的历史的商家,曾经主做低价虚拟主机,也是比较有知名度的,那时候也有接触过,不过一直没有过多的使用。这不这么多年过去,Hostinger商家一直比较稳妥的在运营,最近看到这个商家在改版UI后且产品上也在活动策划比较多。目前Hostinger在进...

unicode转换为你推荐
支持ipad资费标准联通所有套餐介绍如何发帖子请问在网上发帖子怎么发?什么是seoseo标题和seo关键词是什么意思discuz!databaseerrorDiscuz,Database Error是什么原因,怎么修复商务软件软件分为哪几种类型?店铺统计怎样查淘宝店铺的销售总额财务单据我是做财务的,每个月都被各种票据搞得很头疼啊?求各位大神指教好方法!论坛版块图标论坛版块图标怎么设置?放图片的链接吗?还是?超级用户电脑怎么切换超级用户模式
网络星期一 双12活动 轻博 512m内存 铁通流量查询 促正网秒杀 河南m值兑换 网站cdn加速 中国电信测网速 佛山高防服务器 100mbps 搜索引擎提交入口 idc查询 便宜空间 华为云建站 华为k3 双线空间 阿里云手机官网 网站防护 七十九刀 更多