Original post

Hi,

I was trying to encode Chinese characters using utf16.Encode() but it results in an error.

Can anyone please help me in encoding the Chinese characters.

program:-

package main

import (
        "fmt"
        "unicode/utf16"
)

func main() {
        con := "Insert into SAMPLE(ID,NAME,LOCATION,POSITION) values ('3242','中文','hyd','manager')"
        a := stringToUTF16(con)
        b := uTF16ToString(a)
        fmt.Println(b)
}

func stringToUTF16(s string) []uint16 { return utf16.Encode([]rune(s + "u0000")) }

func uTF16ToString(s []uint16) string {
        for i, v := range s {
                if v == 0 {
                        s = s[0:i]
                        break
                }
        }
        return string(utf16.Decode(s))
}

Output:-

![image|690x65](upload://8fUs6roORswU8M1zzUG89XvceOI.png) 

Thanks,
Akhil