Skip to content

Instantly share code, notes, and snippets.

@ajxchapman
Last active April 24, 2022 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajxchapman/824a5470ee6fb5471817162abacfd6d3 to your computer and use it in GitHub Desktop.
Save ajxchapman/824a5470ee6fb5471817162abacfd6d3 to your computer and use it in GitHub Desktop.
Bunch of `hexdump -C` like functions for various languages
// https://gist.github.com/richinseattle/c527a3acb6f152796a580401057c78b4
#include <stdio.h>
#include <ctype.h>
#ifndef HEXDUMP_COLS
#define HEXDUMP_COLS 16
#endif
void hexdump(void *mem, unsigned int len)
{
unsigned int i, j;
for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
{
/* print offset */
if(i % HEXDUMP_COLS == 0)
{
printf("0x%06x: ", i);
}
/* print hex data */
if(i < len)
{
printf("%02x ", 0xFF & ((char*)mem)[i]);
}
else /* end of block, just aligning for ASCII dump */
{
printf(" ");
}
/* print ASCII dump */
if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
{
for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
{
if(j >= len) /* end of block, not really printing */
{
putchar(' ');
}
else if(isprint(((char*)mem)[j])) /* printable char */
{
putchar(0xFF & ((char*)mem)[j]);
}
else /* other char */
{
putchar('.');
}
}
putchar('\n');
}
}
}
package main
import (
"fmt"
"encoding/hex"
)
func main() {
fmt.Print(hex.Dump([]byte{65, 66, 67, 226, 130, 172}))
})
function hexToBytes(hex) {
if (bytes.length != 8) {
throw `${hex} != ${bytes}`;
}
for (var bytes = [], c = hex.length -2; c >= 0; c -= 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function hexdump(buffer) {
blockSize = 16;
var lines = [];
for (var b = 0; b < buffer.length; b += blockSize) {
var block = buffer.slice(b, Math.min(b + blockSize, buffer.length));
var addr = ("0000" + b.toString(16)).slice(-4);
var codes = block.map(ch => ' ' + ch.toString(16).padStart(2, '0')).join('');
codes += " ".repeat(blockSize - block.length);
var chars = block.map(ch => String.fromCharCode(ch)).join('').replace(/[\x00-\x1F\x20]/g, '.');
chars += " ".repeat(blockSize - block.length);
lines.push(addr + " " + codes + " " + chars);
}
return lines.join("\n");
}
"""
https://gist.github.com/7h3rAm/5603718
"""
def hexdump(src, length=16, sep='.'):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or sep for x in range(256)])
lines = []
for c in range(0, len(src), length):
chars = src[c:c+length]
hexstr = ' '.join(["%02x" % ord(x) for x in chars]) if type(chars) is str else ' '.join(['{:02x}'.format(x) for x in chars])
if len(hexstr) > 24:
hexstr = "%s %s" % (hexstr[:24], hexstr[24:])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or sep) for x in chars]) if type(chars) is str else ''.join(['{}'.format((x <= 127 and FILTER[x]) or sep) for x in chars])
lines.append("%08x: %-*s |%s|" % (c, length*3, hexstr, printable))
return '\n'.join(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment