binary string question
Alex Martelli
aleaxit at yahoo.com
Fri Nov 7 18:20:05 EST 2003
Dan Jones wrote:
> Hello,
>
> 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
>
> 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?
You can access an individual byte simply by indexing the string -- for
any string x, x[0] is the first byte, x[1] the second one, and so on. As
to what you mean by "shifting by 8 bits", I dunno -- just slice with
x[1:] is what you perhaps may mean...? For N bits where N is not a
multiple of 8 you have a serious problem, probably best met with
extension module gmpy if performance is important.
Alex
More information about the Python-list
mailing list