Last active
September 10, 2024 13:48
-
-
Save chmike/05da938833328a9a94e02506922f2e7b to your computer and use it in GitHub Desktop.
Go language function to dump a byte slice in hexadecimal and ASCII
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func dumpByteSlice(b []byte) { | |
var a [16]byte | |
n := (len(b) + 15) &^ 15 | |
for i := 0; i < n; i++ { | |
if i%16 == 0 { | |
fmt.Printf("%4d", i) | |
} | |
if i%8 == 0 { | |
fmt.Print(" ") | |
} | |
if i < len(b) { | |
fmt.Printf(" %02X", b[i]) | |
} else { | |
fmt.Print(" ") | |
} | |
if i >= len(b) { | |
a[i%16] = ' ' | |
} else if b[i] < 32 || b[i] > 126 { | |
a[i%16] = '.' | |
} else { | |
a[i%16] = b[i] | |
} | |
if i%16 == 15 { | |
fmt.Printf(" %s\n", string(a[:])) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The instruction
will print the following to stdOut