On Wed Apr 18, 2007 at 14:27:49 +1000, Carlo Sogono wrote:
>Ben Leslie wrote:
>>Also definately recommend the struct class.
>
>I'm trying to use struct.pack to create a single byte with the value 01.
>My two choices are probably x (pad byte) and c (char) but as expected
>none of them worked:
>
>>>> struct.pack('x', 1)
>Traceback (most recent call last):
> File "<stdin>", line 1, in ?
>struct.error: too many arguments for pack format
>
>...because it doesn't want an argument and just defaults to 00 and...
>
>>>> struct.pack('c', 1)
>Traceback (most recent call last):
> File "<stdin>", line 1, in ?
>struct.error: char format require string of length 1
>
>... because 1 is an integer. How do I create a single byte with the
>value 01 (not the ascii '1')?
>
>Sorry for the noob questions. :)
You want 'B' (or 'b').
>>> import struct
>>> struct.pack('B', 37)
'%'
Cheers,
Benno
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders