constructing bytestrings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lenny G.

    #1

    constructing bytestrings

    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\x 00"' 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?

  • Bryan Olson

    #2
    Re: constructing bytestrings

    Lenny G. wrote:[color=blue]
    > 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\x 00"' 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?[/color]


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


    --
    --Bryan

    Comment

    Working...