golang 字符串重新编码
-
golang 字符串重新编码
//byte decode /** * function 对字符串重新编码 * params text:需要编码的字符串 * encodeStr:编码类型;GB18030 GBK HZGB2312 */ func UTF8ReDecode(text string, encodeStr string) (string, error) { var reader io.Reader switch encodeStr { case "GB18030": reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GB18030.NewDecoder()) case "GBK": reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GBK.NewDecoder()) case "HZGB2312": reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.HZGB2312.NewDecoder()) default: reader = transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GB18030.NewDecoder()) } d, err := ioutil.ReadAll(reader) if err != nil { return "", err } return string(d), nil }
有疑问加站长微信联系(非本文作者)