problem with bcd and a number

Dave Angel davea at ieee.org
Thu Aug 4 21:28:58 CEST 2011


On 01/-10/-28163 02:59 PM, nephish wrote:
> Hey all,
>
> I have been trying to get my head around how to do something, but i am
> missing how to pull it off.
> I am reading a packet from a radio over a serial port.
>
> i have " two bytes containing the value i need.  The first byte is the
> LSB, second is MSB.  Both bytes are BCD-encoded, with the LSB
> containing digits zX and MSB containing digits xy.  The system speed
> is then xyz%, where 100% means maximum speed and would be given by
> bytes 00(LSB) 10(MSB)."
>
> that is a quote from the documentation.
> Anyway, i am able to parse out the two bytes i need, but don't know
> where to go from there.
>
> thanks for any tips on this.
>
Your problem is simply to extract the two nibbles from a byte.  Then you 
can use trivial arithmetic to combine the nibbles.  Error checking is 
another matter.

First you need to specify whether this is Python 2.x or Python 3.x.  In 
this message I'll assume 2.7

 >>> val = "\x47"
 >>> print val
G
 >>> print ord(val)
71
 >>> print ord(val)/16
4
 >>> print ord(val)%16
7
 >>>

DaveA





More information about the Python-list mailing list