constructing bytestrings

Bryan Olson fakeaddress at nowhere.org
Fri Aug 12 00:48:00 EDT 2005


Lenny G. wrote:
 > I use 's = os.read(fd, 12)' to grab 12 bytes from a file.  Now, I want
 > to create a string, s1, which contains 16-12=4 bytes: 4,0,0,0, followed
 > by the contents of s.
 >
 > I know how to 's1 = "\x04\x00\x00\x00"' and how to 's3 = s1+s', but I
 > don't know how to construct s1 dynamically (i.e., given N, construct
 > "N" followed by N-1 "0", where "N" and "0" are single byte values with
 > N<16).  How do I do this?


     chr(N) + '\x00' * (N - 1)


-- 
--Bryan




More information about the Python-list mailing list