六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 63|回复: 0

iconv.go,GO编码转换

[复制链接]

升级  48%

6

主题

6

主题

6

主题

童生

Rank: 1

积分
24
 楼主| 发表于 2013-1-19 04:13:44 | 显示全部楼层 |阅读模式
/*Wraps the iconv API present on most systems, which allows for conversionof bytes from one encoding to another. This package additionally providessome convenient interface implementations like a Reader and Writer.*/package iconv/*#include <errno.h>*/import "C"import "os"// Alias os.Error for conveniencetype Error os.Error// Error codes returned from iconv functionsvar (E2BIG Error = os.Errno(int(C.E2BIG))EBADF Error = os.Errno(int(C.EBADF))EINVAL Error = os.Errno(int(C.EINVAL))EILSEQ Error = os.Errno(int(C.EILSEQ))ENOMEM Error = os.Errno(int(C.ENOMEM)))// All in one Convert method, rather than requiring the construction of an iconv.Converterfunc Convert(input []byte, output []byte, fromEncoding string, toEncoding string) (bytesRead int, bytesWritten int, err Error) {// create a temporary converterconverter, err := NewConverter(fromEncoding, toEncoding)if err == nil {// call converter's ConvertbytesRead, bytesWritten, err = converter.Convert(input, output)if err == nil {var shiftBytesWritten int// call Convert with a nil input to generate any end shift sequences_, shiftBytesWritten, err = converter.Convert(nil, output[bytesWritten:])// add shift bytes to total bytesbytesWritten += shiftBytesWritten}// close the converterconverter.Close()}return}// All in one ConvertString method, rather than requiring the construction of an iconv.Converterfunc ConvertString(input string, fromEncoding string, toEncoding string) (output string, err Error) {// create a temporary converterconverter, err := NewConverter(fromEncoding, toEncoding)if err == nil {// convert the stringoutput, err = converter.ConvertString(input)// close the converterconverter.Close()}return}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表