binary string question

Andrew Henshaw andrew.henshaw at gtri.gatech.edu
Mon Nov 10 14:39:11 CET 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?
> 
> Thanks,
> 
> Dan Jones

The other responses address your overall requirements (e.g. use the array
module); but, it looks like you may also need a pointer on this specific
question.  It appears that you have a string of octets (perhaps from
reading a "binary" file) and you want to be able to perform arithmetic
operations on an individual octet.  To do so, use 'ord'.  For example,

>>> print ord('A') << 1
130


-- 
Andy Henshaw




More information about the Python-list mailing list