读书人

iconv.goGO编码变换

发布时间: 2012-12-25 16:18:28 作者: rapoo

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方法是哪来的呀

读书人网 >编程

热点推荐