zvodd
7/16/2014 - 4:31 PM

Make packing string more verbose and human readable.

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")