Skip to content

Instantly share code, notes, and snippets.

@zvodd
Created July 16, 2014 16:31
Show Gist options
  • Save zvodd/0b78ad4bdbba63b3b361 to your computer and use it in GitHub Desktop.
Save zvodd/0b78ad4bdbba63b3b361 to your computer and use it in GitHub Desktop.
Make packing string more verbose and human readable.
def convert(instr, token=','):
longtoshort = {'pad' : 'x',
'char' : 'c',
'schar' : 'b',
'uchar' : 'B',
'byte' : 'B',
'bool' : '?',
'short' : 'h',
'ushort' : 'H',
'int' : 'i',
'uint' : 'I',
'long' : 'l',
'ulong' : 'L',
'long long' : 'q',
'ulong long' : 'Q',
'float' : 'f',
'double' : 'd',
'char*' : 's',
'char*' : 'p',
'void*' : 'P'}
if token == '*':
raise Exception('Invalid token char "%s"' %token)
struct_fmt_str = ''
for word in instr.split(token):
if word and word in longtoshort:
struct_fmt_str += longtoshort[word]
else:
raise Exception('Bad format string baby: "%s" is not in my lexicon' %word )
return struct_fmt_str
print convert("byte,byte,byte,pad,uint,ulong,pad,pad,byte,int")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment