Created
July 16, 2014 16:31
-
-
Save zvodd/0b78ad4bdbba63b3b361 to your computer and use it in GitHub Desktop.
Make packing string more verbose and human readable.
This file contains 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
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