Ho to encode chinese character to utf-16

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:-

Thanks,
Akhil

1 Like

Your code is good. In the Go Playground (Linux): https://play.golang.org/p/BZAPsAgEmo6.

Your code is similar to the code in package windows: golang.org/x/sys/windows,

// UTF16FromString returns the UTF-16 encoding of the UTF-8 string
// s, with a terminating NUL added. If s contains a NUL byte at any
// location, it returns (nil, syscall.EINVAL).
func UTF16FromString(s string) ([]uint16, error) {
	for i := 0; i < len(s); i++ {
		if s[i] == 0 {
			return nil, syscall.EINVAL
		}
	}
	return utf16.Encode([]rune(s + "\x00")), nil
}

// UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,
// with a terminating NUL removed.
func UTF16ToString(s []uint16) string {
	for i, v := range s {
		if v == 0 {
			s = s[0:i]
			break
		}
	}
	return string(utf16.Decode(s))
}

Your problem is Microsoft Windows.

The Windows Command Prompt (cmd.exe) needs to use a font that displays Chinese characters. NSimSun or SimSun-ExtB works for me.

Also, the new Microsoft Windows Terminal (Microsoft Store) works for me.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.