Skip to content

Instantly share code, notes, and snippets.

@riptl
Created September 3, 2017 13:27
Show Gist options
  • Save riptl/c2e1f1a3a11cfcc9eb308ef7597a983f to your computer and use it in GitHub Desktop.
Save riptl/c2e1f1a3a11cfcc9eb308ef7597a983f to your computer and use it in GitHub Desktop.
Python 2 hex converter for OSdev
#!/usr/bin/python
# Python 2 format converter
from __future__ import division
import sys
sizes = {
'bit': 0.125,
'kbit': 125,
'mbit': 125000,
'b': 1,
'kb': 1000,
'kib': 1024,
'mb': 1000000,
'mib': 1048576
}
def parseDecimal(arg):
return int(arg)
def parseHex(arg):
# Drop 0x
literal = arg[2:] if arg.startswith('0x') else arg
return int(literal, 16)
def parseSize(format):
def high(arg):
return int(arg) * sizes[format]
return high
def exportSize(format):
def high(arg):
return str(arg / sizes[format]) + ' ' + format
return high
if len(sys.argv) <= 2:
print >> sys.stderr, 'Not enough args.'
exit(1)
inmode = sys.argv[1]
outmode = sys.argv[2]
if len(sys.argv) == 3:
value = 1
else:
value = sys.argv[3]
try:
infunc = {
'hex': parseHex,
'dec': parseDecimal
}[inmode]
except KeyError:
infunc = parseSize(inmode)
try:
outfunc = {
'hex': hex,
'dec': str
}[outmode]
except KeyError:
outfunc = exportSize(outmode)
print outfunc(infunc(value))
@riptl
Copy link
Author

riptl commented Sep 3, 2017

Usage: hconv <informat> <outformat> [value=1]

Available formats:

  • hexadecimal (e.g. for addresses)
  • decimal (e.g. for struct sizes)
  • bit
  • kbit (Kilobit)
  • mbit (Megabit)
  • b (byte)
  • kb (Kilobyte, KB = 1000 byte)
  • kib (Kibibyte, KiB = 1024 byte)
  • mb (Megabyte, MB = 1000000 byte)
  • mib (Mebibyte, MiB = 1048576 byte)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment