1 回答

TA貢獻1784條經驗 獲得超8個贊
charmap.ISO8859_9.NewEncoder().Bytes() 函數想要 UTF-8 格式進行編碼。當我嘗試對字節進行編碼時出現錯誤。因為我傳入的字節是 8859-9 格式,我試圖直接轉換它們。首先,我將字節解碼為 UTF-8 格式。我完成了我的過程,最后我使用編碼器將這個 UTF-8 字節編碼為 ISO8859-9 unicode。這是新代碼。
//main package
bytes, err := textproc.R.ReadBytes(constant.EndTextDelimiter)
checkError(err)
msg := encoder.DecodeISO8859_9ToUTF8(bytes)
//..........
// Process that string, create struct Then convert struct to json bytes
// Then encode that bytes
json := encoder.EncodeUTF8ToISO8859_9(bytes)
//encoder package
package encoder
import "golang.org/x/text/encoding/charmap"
func DecodeISO8859_9ToUTF8(bytes []byte) string {
encoded, _ := charmap.ISO8859_9.NewDecoder().Bytes(bytes)
return string(encoded[:])
}
func EncodeUTF8ToISO8859_9(bytes []byte) string {
encoded, _ := charmap.ISO8859_9.NewEncoder().Bytes(bytes)
return string(encoded[:])
}
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報