func printLine(addr int, buf []byte, sz int, wr io.Writer, end int) { _, err := io.WriteString(wr, iching.SpacePad(iching.Itoiching(uint64(addr)), sz)) if err != nil { fmt.Fprintf(os.Stderr, "Error on output: %v\n", err) os.Exit(1) } io.WriteString(wr, " ") for i := 0; i < len(buf); i += 2 { n := uint64(buf[i])<<8 + uint64(buf[i+1]) if i == 8 { io.WriteString(wr, " ") } if i >= end { fmt.Fprintf(wr, " ") } else { ich := iching.QianPad(iching.Itoiching(n), 3) fmt.Fprintf(wr, " %s", ich) } } io.WriteString(wr, " |") for i := 0; i < len(buf); i++ { if i >= end { fmt.Fprintf(wr, " ") } else { if buf[i] >= 0x20 && buf[i] <= 0x7e { fmt.Fprintf(wr, "%c", buf[i]) } else { io.WriteString(wr, ".") } } } io.WriteString(wr, "|\n") }
func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "Wrong number of arguments.\n") usage() } s := os.Args[1] var n uint64 var err error ch := ([]rune(s))[0] if ch >= 0x4dc0 && ch <= 0x4dff { n, err = iching.Ichingtoi(s) } else { n, err = strconv.ParseUint(s, 0, 64) } if err != nil { fmt.Fprintf(os.Stderr, "Wrong argument: %v\n", err) usage() } fmt.Printf("decimal = %d\n", n) oct := fmt.Sprintf("%o", n) fmt.Printf("octal =") start := 0 if len(oct)%2 != 0 { fmt.Printf(" 0%c", oct[0]) start = 1 } for i := start; i < len(oct); i += 2 { fmt.Printf(" %c%c", oct[i], oct[i+1]) } fmt.Printf("\n") ich := iching.Itoiching(n) fmt.Printf("iching =") for _, ch := range []rune(ich) { fmt.Printf(" %c", ch) } fmt.Printf("\n") }
func main() { if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "Wrong number of arguments\n") usage() } fh, err := os.Open(os.Args[1]) if err != nil { fmt.Fprintf(os.Stderr, "Could not open %s: %v\n", os.Args[1], err) usage() } fi, err := fh.Stat() if err != nil { fmt.Fprintf(os.Stderr, "Could not stat %s: %v\n", os.Args[1], err) usage() } sz := len([]rune(iching.Itoiching(uint64(fi.Size())))) rd := bufio.NewReader(fh) wr := bufio.NewWriter(os.Stdout) defer wr.Flush() buf := make([]byte, 16) i := 0 for { n, err := io.ReadFull(rd, buf) for i := n; i < len(buf); i++ { buf[i] = 0x00 } if n > 0 { printLine(i, buf, sz, wr, n) i += n } if err != nil { if err != io.EOF && err != io.ErrUnexpectedEOF { fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err) os.Exit(1) } break } } }