blob: 9f27696bfb9bc43bf9af30712ef6b4c355762d4a [file] [log] [blame]
Nigel Tao79a94552017-11-30 16:37:20 +11001// Copyright 2017 The Wuffs Authors.
Nigel Taod4372cb2017-10-12 11:17:41 +11002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Nigel Taoc463f492017-07-17 17:28:19 +100014
Nigel Tao788479d2021-08-22 10:52:51 +100015//go:build ignore
Nigel Taoc463f492017-07-17 17:28:19 +100016// +build ignore
17
18package main
19
20// print-bits.go prints stdin's bytes in hexadecimal and binary.
21//
22// Usage: go run print-bits.go < foo.bin
23
24import (
25 "fmt"
26 "io"
27 "os"
28)
29
30func main() {
31 if err := main1(); err != nil {
32 os.Stderr.WriteString(err.Error() + "\n")
33 os.Exit(1)
34 }
35}
36
37func main1() error {
38 in := make([]byte, 4096)
39 bits := []byte("...._....\n")
Nigel Taoadadb0f2017-07-20 15:36:59 +100040 os.Stdout.WriteString("offset xoffset ASCII hex binary\n")
Nigel Taoc463f492017-07-17 17:28:19 +100041 for iBase := 0; ; {
42 n, err := os.Stdin.Read(in)
43 for i, x := range in[:n] {
44 bits[0] = bit(x >> 7)
45 bits[1] = bit(x >> 6)
46 bits[2] = bit(x >> 5)
47 bits[3] = bit(x >> 4)
48 bits[5] = bit(x >> 3)
49 bits[6] = bit(x >> 2)
50 bits[7] = bit(x >> 1)
51 bits[8] = bit(x >> 0)
52 ascii := rune(x)
53 if (x < 0x20) || (0x7F <= x) {
54 ascii = '.'
55 }
Nigel Taoadadb0f2017-07-20 15:36:59 +100056 fmt.Printf("%06d 0x%04X %c 0x%02X 0b_%s", iBase+i, iBase+i, ascii, x, bits)
Nigel Taoc463f492017-07-17 17:28:19 +100057 }
58 if err == io.EOF {
59 return nil
60 }
61 if err != nil {
62 return err
63 }
64 iBase += n
65 }
66}
67
68func bit(x byte) byte {
69 if x&1 != 0 {
70 return '1'
71 }
72 return '0'
73}