simpleJSON pack binary data

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Jul 22 11:10:42 CEST 2007


On Sat, 21 Jul 2007 19:13:22 -0700, Andrey wrote:

> My question is, anyone will suggest a workaround to this error?
> i really like to pack my raw image data into the JSON, so my other 
> programming script can read the array easily

JSON is a text format so you have to encode the binary data somehow.  I'd
use base64.  It's available as codec for `str.encode()`/`str.decode()`.

In [10]: '\x00\xff\xaa'
Out[10]: '\x00\xff\xaa'

In [11]: '\x00\xff\xaa'.encode('base64')
Out[11]: 'AP+q\n'

In [12]: _.decode('base64')
Out[12]: '\x00\xff\xaa'

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list