mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 00:47:28 +01:00
Add script/print-bits.go.
This commit is contained in:
1 parent
547351cfec
commit
c463f4956f
2 files changed
+62
-1
No files matched your search
@@ -0,0 +1,61 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
// print-bits.go prints stdin's bytes in hexadecimal and binary.
|
||||||
|
//
|
||||||
|
// Usage: go run print-bits.go < foo.bin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := main1(); err != nil {
|
||||||
|
os.Stderr.WriteString(err.Error() + "\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main1() error {
|
||||||
|
in := make([]byte, 4096)
|
||||||
|
bits := []byte("...._....\n")
|
||||||
|
os.Stdout.WriteString("offset ASCII hex binary\n")
|
||||||
|
for iBase := 0; ; {
|
||||||
|
n, err := os.Stdin.Read(in)
|
||||||
|
for i, x := range in[:n] {
|
||||||
|
bits[0] = bit(x >> 7)
|
||||||
|
bits[1] = bit(x >> 6)
|
||||||
|
bits[2] = bit(x >> 5)
|
||||||
|
bits[3] = bit(x >> 4)
|
||||||
|
bits[5] = bit(x >> 3)
|
||||||
|
bits[6] = bit(x >> 2)
|
||||||
|
bits[7] = bit(x >> 1)
|
||||||
|
bits[8] = bit(x >> 0)
|
||||||
|
ascii := rune(x)
|
||||||
|
if (x < 0x20) || (0x7F <= x) {
|
||||||
|
ascii = '.'
|
||||||
|
}
|
||||||
|
fmt.Printf("%06d %c 0x%02X 0b_%s", iBase+i, ascii, x, bits)
|
||||||
|
}
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
iBase += n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func bit(x byte) byte {
|
||||||
|
if x&1 != 0 {
|
||||||
|
return '1'
|
||||||
|
}
|
||||||
|
return '0'
|
||||||
|
}
|
||||||
@@ -237,7 +237,7 @@ char* hex_dump(char* msg, puffs_base_buf1* buf, size_t i) {
|
|||||||
char c = ' ';
|
char c = ' ';
|
||||||
if (k < n) {
|
if (k < n) {
|
||||||
c = buf->ptr[b + k];
|
c = buf->ptr[b + k];
|
||||||
if ((c < 0x20) || (0x7F < c)) {
|
if ((c < 0x20) || (0x7F <= c)) {
|
||||||
c = '.';
|
c = '.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user