Last active
February 9, 2024 06:58
-
-
Save Bablzz/acfec39aceb84ee3a8a9614a50c87eac to your computer and use it in GitHub Desktop.
Convert string UTF-8 to UTF-16LE golang
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
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