Packing data

bvdpoel at uniserve.com bvdpoel at uniserve.com
Wed May 1 20:15:10 EDT 2002


I'm doing some midi stuff and need to pack ints into a byte string. I'm
doing

def mkstring(var):
 ret = ""
 for a in var:
  ret += chr(a)
 return ret

Which works. But I got to thinking there might be a more efficient way
to to this. So I started to look at array and struct.

I figured that struct.pack('b', var) might work ... but var can't be a
list and the fmt string has to be the right size. So, I can do something
like:

	struct.pack('bbb', v1, v2, v3)

but a more general implimentation escapes my small brain.




-- 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at uniserve.com
WWW:   http://users.uniserve.com/~bvdpoel




More information about the Python-list mailing list