Skip to content

Instantly share code, notes, and snippets.

Last active November 29, 2023 22:08
  • Star 31 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
golang, convert UTF-16 to UTF-8 string
package main
import "fmt"
import "unicode/utf16"
import "unicode/utf8"
import "bytes"
func main() {
b := []byte{
0xff, // BOM
0xfe, // BOM
s, err := DecodeUTF16(b)
if err != nil {
func DecodeUTF16(b []byte) (string, error) {
if len(b)%2 != 0 {
return "", fmt.Errorf("Must have even length byte slice")
u16s := make([]uint16, 1)
ret := &bytes.Buffer{}
b8buf := make([]byte, 4)
lb := len(b)
for i := 0; i < lb; i += 2 {
u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8)
r := utf16.Decode(u16s)
n := utf8.EncodeRune(b8buf, r[0])
return ret.String(), nil
Copy link

wenjy commented Oct 13, 2020

Thanks very much!

Copy link

maxneo4 commented Mar 11, 2021

golang already has support for decoding []byte into []uint16 (respecting the endianness):

func DecodeUtf16(b []byte, order binary.ByteOrder) (string, error) {
	ints := make([]uint16, len(b)/2)
	if err := binary.Read(bytes.NewReader(b), order, &ints); err != nil {
		return "", err
	return string(utf16.Decode(ints)), nil

@akirabbq @ik5
complete solution (which also works with surrogate pairs): utf16.go

You send just function I need to convert clob Oracle data to string.

Copy link

korau commented Jan 7, 2022

Life saver, thanks

Copy link

marians commented Sep 28, 2022

This helped me to decode UTF-16LE to UTF-8:

decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
utf8bytes, err := decoder.Bytes(data) // data contains UTF16LE as read from a file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment