用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
hailongchang的公告
<center> <a href="http://www3.clustrmaps.com/counter/maps.php?url=http://blog.csdn.net/hailongchang" id="clustrMapsLink"><img src="http://www3.clustrmaps.com/counter/index2.php?url=http://blog.csdn.net/hailongchang" style="border:0px;" alt="Locations of visitors to this page" title="Locations of visitors to this page" id="clustrMapsImg" onError="this.onError=null; this.src='http://www2.clustrmaps.com/images/clustrmaps-back-soon.jpg'; document.getElementById('clustrMapsLink').href='http://www2.clustrmaps.com'" /> </a></center> <br><br> <h5>最近在读...</h5><br> <center> <a href="http://www.amazon.com/Structure-Interpretation-Computer-Programs-Engineering/dp/0262011530/sr=8-1/qid=1164689869/ref=pd_bbs_1/002-5995427-4320807?ie=UTF8&s=books"> <img title='Structure and Interpretation of Computer Programs' width=175px height=150px src="http://ec1.images-amazon.com/images/P/0262011530.01._AA240_SCLZZZZZZZ_.jpg" border="0"> </a> <a href="http://www.amazon.com/o/ASIN/0072899050/ref=pd_rvi_gw_1/002-5995427-4320807"> <img title='Discrete Mathematics and its Apllications' width=175px height=150px src="http://ec1.images-amazon.com/images/P/0072899050.01._SS500_SCLZZZZZZZ_V1056416421_.jpg" border="0"> </a> <a href="http://www.amazon.com/Introduction-Algorithms-Thomas-H-Cormen/dp/0262032937"> <img title='Introduction to Algorithms' width=140px height=150px src="http://ec1.images-amazon.com/images/I/41WDWECWVCL._AA240_.jpg" border="0"> </a> <a href="http://www.china-pub.com/computers/common/info.asp?id=24853"> <img title='微积分和数学分析引论' width=120px height=150px src="http://www.china-pub.com/computers/ebook20001-25000/24853/zcover.gif" border="0"> </a></center>
文章分类
Linux&Unix
兰州大学开源社区
Mathematics
Mathematica Tutorial
博士家园论坛
Programming
CodeProject
存档

原创  Unix下UTF-8和GB2312互相转换的函数 收藏

    在windows下进行utf-8和gb2312相互转化的代码网上流传了很广了,因为实现里面使用了两个windows函数(WideCharToMultiByte 和 MultiByteToWideChar),在Unix下编程就没有办法使用了,上网google研究了下,发现在Unix下就是对iconv库功能的调用,非常简便。
    详细的信息,可以man 3 iconv查看,这里列出我写的一个演示程序。
  1. #include<iostream>
  2. #include<iconv.h>
  3. using namespace std;
  4. int utf8togb2312(const char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
  5. {
  6.   iconv_t cd;
  7.   if( (cd = iconv_open("gb2312","utf-8")) ==0 )
  8.     return -1;
  9.   memset(destbuf,0,destlen);
  10.   const char **source = &sourcebuf;
  11.   char **dest = &destbuf;
  12.   if(-1 == iconv(cd,source,&sourcelen,dest,&destlen))
  13.     return -1;
  14.   iconv_close(cd);
  15.   return 0;
  16.   
  17. }
  18. int gb2312toutf8(const char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
  19. {
  20.   iconv_t cd;
  21.   if( (cd = iconv_open("utf-8","gb2312")) ==0 )
  22.     return -1;
  23.   memset(destbuf,0,destlen);
  24.   const char **source = &sourcebuf;
  25.   char **dest = &destbuf;
  26.   if(-1 == iconv(cd,source,&sourcelen,dest,&destlen))
  27.     return -1;
  28.   iconv_close(cd);
  29.   return 0;
  30.   
  31. }
  32. int main()
  33. {
  34.   FILE *fp = fopen("love.txt","r");
  35.   if(fp == NULL)
  36.     {
  37.       cout << "open file faiulre" << endl;
  38.       return 1;
  39.     }
  40.   char line[80];
  41.   memset(line,0,80);
  42.   if( NULL == fgets(line,80,fp))
  43.     {
  44.       cout << "read file failure" << endl;
  45.       return 2;
  46.     }
  47.   char dest_gb2312[80];
  48.   utf8togb2312(line,strlen(line),dest_gb2312,80);
  49.   cout << "before convert(utf-8): " << line << endl;
  50.   cout << "after convert(gb2312): " << dest_gb2312 << endl;
  51.   cout << endl << endl;
  52.   char dest_utf8[80];
  53.   gb2312toutf8(dest_gb2312,strlen(dest_gb2312),dest_utf8,80);
  54.   cout << "before convert(gb2312)" << dest_gb2312 << endl;
  55.   cout << "after convert(utf-8)" << dest_utf8 << endl;
  56.     return 0;
  57. }
    首先我建立了一个文本文件,保存有字符串"我爱北京天安门",然后存为utf-8格式,在FreeBSD下编译,运行程序,看起来是这个样子:




发表于 @ 2008年12月24日 14:26:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:隐藏Windows系统托盘图标(改进版) | 新一篇:用shell脚本批量连续更改FreeBSD主机ip

  • 发表评论
  • 评论内容:
  •  
Copyright © hailongchang
Powered by CSDN Blog