1. 4FIPS
  2. PHOTOS
  3. VIDEOS
  4. APPS
  5. CODE
  6. FORUMS
  7. ABOUT
#!/usr/bin/env python

# /*
# (c) 2014 +++ Filip Stoklas, aka FipS, http://www.4FipS.com +++
# THIS CODE IS FREE - LICENSED UNDER THE MIT LICENSE
# ARTICLE URL: http://forums.4fips.com/viewtopic.php?f=3&t=1210
# */

from string import printable

def bindump(data, width=16, fmt="%02X", sep=" ", tx_on=True, tx_sep="| ", tx_chars=printable[:-5]):
    def chunks(l, n):
        for i in xrange(0, len(l), n):
            yield l[i:i+n]
    res = ""
    tx_indent = width * (len(fmt % 255) + len(sep))
    for line in chunks(data, width):
        hx = "".join("%s%s" % (fmt % ord(c), sep) for c in line)
        tx = tx_sep + "".join((".", c)[c in tx_chars] for c in line)
        res += "%s%s\n" % (hx.ljust(tx_indent, " "), tx if tx_on else "")
    return res

def hexdump_c(data, width=16, fmt="0x%02X", sep=", ", tx_on=True, tx_sep="// "):
    return bindump(data, width, fmt, sep, tx_on, tx_sep)

def decdump_c(data, width=16, fmt="%d", sep=",", tx_on=True, tx_sep=" // "):
    return bindump(data, width, fmt, sep, tx_on, tx_sep)

if __name__ == "__main__":
    data = "".join([chr(x) for x in range(0, 256)]) + "Hello World !!!\n\n\n\\/"
    print(bindump(data))
    print(hexdump_c(data))
    print(hexdump_c(data, tx_on=False))
    print(decdump_c(data))
    print(decdump_c(data, tx_on=False))