question about binary and serial info

John Machin sjmachin at
Fri Aug 19 03:53:05 CEST 2005

Grant Edwards wrote:
> On 2005-08-18, nephish at <nephish at> wrote:
>>i have an ascii string comming in the serial port and i need to convert
>>it to something else, like an integer, or binary, or even a hex so i
>>can use the bitwise comparison on it.

Nephish, *WHY* do want to use "the bitwise comparison" on it? What do 
you understand a "bitwise comparison" to be?

> But what do you mean by "integer", "binary", and "hex"?  
> Decimal, hex, and binary are all representations of integers.
> On your computer all integers are 2's compliment binary


Try: sometimes, integers are represented in 2's complEment binary form; 
sometimes they are represented in other forms such as unsigned binary. 
For example an 8-bit byte can be use to represent integers in 
range(-128, 128) using (signed) twos complement binary, or integers in 
range(0, 256) using signed binary, or integers in the range (0, 10) 
using decimal ASCII codes, or integers in range (0, 100) using BCD with 
no sign nibble, or ...

> (we're
> going to ignore BCD for the moment).

> The only thing that can be "decimal" or "hex" are _string_
> representations of integer values.
> If you want something upon which you can perform the bitwise
> boolean operations &, ^, |, then you most probably just want an
> integer object.
> To convert a string to an integer, use the int() builtin
> passing it the string an an optional base:

The OP is reading raw output from a serial port. He's already said he's 
getting "funny ASCII characters". One gets the impression he thinks he 
needs to do bit-twiddling on *each* byte. Looks like he needs ord(), or 
(better) struct.unpack()

(1) Please tell us what docs you have on what the serial gadget is 
outputting, in what format.
(2) Please show us what code you are using to read the serial port.
(3) Please show us the results of
print repr(some_results_of_reading_from_the_serial_port)
(4) Please tell us what you like to transform (3) into, or what you need 
to "bitwise" compare it to.
Then we might be able to give you some coherent advice.

More information about the Python-list mailing list