binary string question

Peter Hansen peter at
Sat Nov 8 00:22:41 CET 2003

Dan Jones wrote:
> I'm trying to figure out how to get bit operators to work on a binary
> string. This is what I'm trying to do:
> list(frame) #where frame is a binary string
> y = frame[x] << 8

You don't describe what you want this to do.  How about an example, showing
input and desired output?

> It gives me a TypeError. Whats the best way to get around that? I think
> the struct module may be what I'm looking for, if so could someone give
> me an example? Also, is there a better way to be able to access
> individual bytes than converting the string to a list?

Yes, use an array (see the module by that name). 

On the other hand, depending on what you really want, the answer might
be as simple as taking your string and skipping the first element.
After all, shifting each byte in a long sequence to the left by 8 bits
is pretty much synonymous with that approach:

  out = in[1:] + '\x00'

But I suspect that's not really what you meant.  Please explain.


More information about the Python-list mailing list