Skip to content

Instantly share code, notes, and snippets.

@hucsmn
Last active August 29, 2015 14:05
Show Gist options
  • Save hucsmn/9c101dd78666d0c3bbe0 to your computer and use it in GitHub Desktop.
Save hucsmn/9c101dd78666d0c3bbe0 to your computer and use it in GitHub Desktop.
查看标准输入 (UTF-8) 里每个字符的 Unicode 编号,类似于 od
/* 查看标准输入 (UTF-8) 里每个字符的 Unicode 编号,类似于 od */
package main
import (
"bufio"
"fmt"
"io"
"os"
)
const col = 8
func main() {
inbuf := bufio.NewReader(os.Stdin)
outbuf := bufio.NewWriter(os.Stdout)
defer outbuf.Flush()
line := make([]rune, 0, col)
for {
r, _, err := inbuf.ReadRune()
if err == io.EOF {
break
}
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s", os.Args[0], err)
os.Exit(1)
}
fmt.Fprintf(outbuf, "%.4x ", r)
line = append(line, r)
if len(line) >= col {
fmt.Fprintf(outbuf, "%q\n", string(line))
line = line[:0]
}
}
if len(line) != 0 {
for i := len(line); i < col; i++ {
fmt.Fprint(outbuf, " ")
}
fmt.Fprintf(outbuf, "%q\n", string(line))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment