Forked from bradleypeabody/gist:185b1d7ed6c0c2ab6cec
Last active
September 1, 2015 11:17
-
-
Save stephenwithav/262ae03755c314465a47 to your computer and use it in GitHub Desktop.
golang, convert UTF-16 to UTF-8 string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// http://play.golang.org/p/fVf7duRtdH | |
import "fmt" | |
import "unicode/utf16" | |
import "encoding/binary" | |
func main() { | |
b := []byte{ | |
0xff, // BOM | |
0xfe, // BOM | |
'T', | |
0x00, | |
'E', | |
0x00, | |
'S', | |
0x00, | |
'T', | |
0x00, | |
0x6C, | |
0x34, | |
'\n', | |
0x00, | |
} | |
s, err := DecodeUTF16(b, binary.LittleEndian) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(s) | |
} | |
// Assume byte slice is the correct length | |
func DecodeUTF16(b []byte, order binary.ByteOrder) (string, error) { | |
u16s := []uint16{} | |
for i, j := 0, len(b); i < j; i += 2 { | |
u16s = append(u16s, order.Uint16(b[i:])) | |
} | |
runes := utf16.Decode(u16s) | |
return string(runes), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment