question about binary and serial info
Grant Edwards
grante at visi.com
Thu Aug 18 13:30:05 EDT 2005
On 2005-08-18, nephish at xit.net <nephish at xit.net> 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.
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 (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:
>>> int("1234")
1234
>>> int("1234",10)
1234
>>> int("0x100",16)
256
>>> int("100",16)
256
>>> int("100",2)
4
Others have already shown you how to use the bitwise boolean
operators.
--
Grant Edwards grante Yow! Look DEEP into the
at OPENINGS!! Do you see any
visi.com ELVES or EDSELS... or a
HIGHBALL??...
More information about the Python-list
mailing list