diff options
| author | Shulhan <ms@kilabit.info> | 2021-07-12 02:09:47 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-07-12 02:09:47 +0700 |
| commit | a15f96aae0574de614b8ab33cfbb09fd70f00182 (patch) | |
| tree | 5315538c00215307d5546eb196585224e686e361 /lib/bytes/bytes.go | |
| parent | 0cd813b8ae8a990a311cb156359edbcf05e77689 (diff) | |
| download | pakakeh.go-a15f96aae0574de614b8ab33cfbb09fd70f00182.tar.xz | |
lib/bytes: print the ASCII character on the right side on PrintHex
Previously, PrintHex only print the hex value of all bytes with
specified length.
This changes also print any printables ASCII characters (char 33 through
126) in the right side column to view readable contents.
Diffstat (limited to 'lib/bytes/bytes.go')
| -rw-r--r-- | lib/bytes/bytes.go | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index 4753629f..6c1e6db8 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -263,14 +263,43 @@ func IsTokenAt(line, token []byte, p int) bool { // length. // func PrintHex(title string, data []byte, col int) { + var ( + start, x int + ) fmt.Print(title) - for x := 0; x < len(data); x++ { + for x = 0; x < len(data); x++ { if x%col == 0 { + if x > 0 { + fmt.Print(" ||") + } + for y := start; y < x; y++ { + if data[y] >= 33 && data[y] <= 126 { + fmt.Printf(" %c", data[y]) + } else { + fmt.Print(" .") + } + } fmt.Printf("\n%4d -", x) + start = x } fmt.Printf(" %02X", data[x]) } + rest := 16 - (x % col) + if rest > 0 { + for y := 0; y < rest; y++ { + fmt.Print(" ") + } + fmt.Print(" ||") + } + for y := start; y < x; y++ { + if data[y] >= 33 && data[y] <= 126 { + fmt.Printf(" %c", data[y]) + } else { + fmt.Print(" .") + } + } + fmt.Println() } |
