Reading a utf-16 text file

Hello all.
I need work with information, which written in UTF-16 file.
I’m new in Go, but heard that he is good Unicode support.
So my task is to extract the text in string for further work.
I tried to do the conversion in the forehead, but it turned out badly:

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	utf16 "unicode/utf16"
)

func main() {

	file, err := os.Open("in.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	fscanner := bufio.NewScanner(file)
	for fscanner.Scan() {
		arrbytes := fscanner.Bytes()
		slice := make([]uint16, len(arrbytes))
		for i := range arrbytes {
			slice[i] = uint16(arrbytes[i])
		}
		newarr := utf16.Decode(slice)
		fmt.Println(string(newarr))
	}

}

I would appreciate any help on this issue.

Note, put your code on play.golang.org so people can get the code. You can use base64 to encode the filedata if necessary. Check out golang.org/x/text package. https://play.golang.org/p/-9t0D_DFTA might work, I don’t have a UTF16 file laying around to test.

2 Likes

thanks, this really works.

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