Skip to content

Instantly share code, notes, and snippets.

@Bablzz
Last active February 9, 2024 06:58
Show Gist options
  • Save Bablzz/acfec39aceb84ee3a8a9614a50c87eac to your computer and use it in GitHub Desktop.
Save Bablzz/acfec39aceb84ee3a8a9614a50c87eac to your computer and use it in GitHub Desktop.
Convert string UTF-8 to UTF-16LE golang
const BOM = '\ufffe' //LE. for BE '\ufeff'
func createFile(name string) error {
var bytes [2]byte
data := `test string UTF-8`
file, err := os.Create(name)
if err != nil {
fmt.Errorf("Can't open file. %v", err)
return err
}
defer file.Close()
bytes[0] = BOM >> 8
bytes[1] = BOM & 255
file.Write(bytes[0:])
runes := utf16.Encode([]rune(data))
for _, r := range runes {
bytes[1] = byte(r >> 8)
bytes[0] = byte(r & 255)
file.Write(bytes[0:])
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment