Velocity Reviews > problem with bcd and a number

# problem with bcd and a number

nephish
Guest
Posts: n/a

 08-04-2011
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.

Dave Angel
Guest
Posts: n/a

 08-04-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

Christoph Hansen
Guest
Posts: n/a

 08-04-2011
nephish schrieb:

> thanks for any tips on this.

I'll try.

In BCD a (decimal) digit is stored in a halfbyte (or a 'nibble'). So, in
a byte
you can store two decimal digits. For instance 42 would be

nibble1 nibble2
0100 0010
4 2

>>> c=0b01000010
>>> c

66
>>> c >> 4 # first nibble

4
>>> c & 0b1111 # second nibble

2

So, a speed of 57% should be
LSB= 0111 0000
MSB= 0000 0101

Chris Angelico
Guest
Posts: n/a

 08-05-2011
On Fri, Aug 5, 2011 at 1:40 AM, Dan Stromberg <(E-Mail Removed)> wrote:
>>>> print int(hex(0x72).replace('0x', ''))

> 72

Or simpler: int(hex(0x72)[2:])

Although if you have it as a string, you need to ord() the string.

It's probably better to just do the bitwise operations though.

ChrisA

Peter Otten
Guest
Posts: n/a

 08-05-2011
Chris Angelico wrote:

> On Fri, Aug 5, 2011 at 1:40 AM, Dan Stromberg <(E-Mail Removed)> wrote:
>>>>> print int(hex(0x72).replace('0x', ''))

>> 72

>
> Or simpler: int(hex(0x72)[2:])
>
> Although if you have it as a string, you need to ord() the string.

Or use str.encode():

>>> int("\x72".encode("hex"))

72
>>> int("\x12\x34\x56".encode("hex"))

123456

> It's probably better to just do the bitwise operations though.