Russell Warren schrieb: > I've got a case where I want to convert binary blocks of data (various > ctypes objects) to base64 strings. > > The conversion calls in the base64 module expect strings as input, so > right now I'm converting the binary blocks to strings first, then > converting the resulting string to base64. This seems highly > inefficient and I'd like to just go straight from binary to a base64 > string. [...] > Is there a canned/pre-existing way to convert a block of memory to a > base64 string more efficiently? I'd like to avoid writing my own > base64 conversion routine if possible. Anyone have any good ideas? > Even a mroe efficient/less clunky way of conevrting an arbitrary object > to a string would be appreciated.
Many functions that operate on strings also accept buffer objects as parameters, this seems also be the case for the base64.encodestring function. ctypes objects support the buffer interface. So, base64.b64encode(buffer(ctypes_instance)) should work efficiently. Thomas -- http://mail.python.org/mailman/listinfo/python-list