首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

iconv.go,GO编码变换

2012-12-25 
iconv.go,GO编码转换/*Wraps the iconv API present on most systems, which allows for conversionof byt

iconv.go,GO编码转换

/*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}
1 楼 ysw1983 2011-04-14   博主,请问你的NewConverter方法是哪来的呀

热点排行