Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10868 > unrolled thread

problem with bcd and a number

Started bynephish <nephish@gmail.com>
First post2011-08-04 11:31 -0700
Last post2011-08-05 17:52 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  problem with bcd and a number nephish <nephish@gmail.com> - 2011-08-04 11:31 -0700
    Re: problem with bcd and a number Dave Angel <davea@ieee.org> - 2011-08-04 15:28 -0400
    Re: problem with bcd and a number Christoph Hansen <ch@radamanthys.de> - 2011-08-04 21:31 +0200
    Re: problem with bcd and a number Chris Angelico <rosuav@gmail.com> - 2011-08-05 16:32 +0100
    Re: problem with bcd and a number Peter Otten <__peter__@web.de> - 2011-08-05 17:52 +0200

#10868 — problem with bcd and a number

Fromnephish <nephish@gmail.com>
Date2011-08-04 11:31 -0700
Subjectproblem with bcd and a number
Message-ID<a59a0314-4fab-4a57-a648-daa2570f2f94@v7g2000vbk.googlegroups.com>
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.

[toc] | [next] | [standalone]


#10870

FromDave Angel <davea@ieee.org>
Date2011-08-04 15:28 -0400
Message-ID<mailman.1899.1312486154.1164.python-list@python.org>
In reply to#10868
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

[toc] | [prev] | [next] | [standalone]


#10872

FromChristoph Hansen <ch@radamanthys.de>
Date2011-08-04 21:31 +0200
Message-ID<j1es27$e64$1@online.de>
In reply to#10868
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

[toc] | [prev] | [next] | [standalone]


#10909

FromChris Angelico <rosuav@gmail.com>
Date2011-08-05 16:32 +0100
Message-ID<mailman.1928.1312558326.1164.python-list@python.org>
In reply to#10868
On Fri, Aug 5, 2011 at 1:40 AM, Dan Stromberg <drsalists@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#10910

FromPeter Otten <__peter__@web.de>
Date2011-08-05 17:52 +0200
Message-ID<mailman.1929.1312559562.1164.python-list@python.org>
In reply to#10868
Chris Angelico wrote:

> On Fri, Aug 5, 2011 at 1:40 AM, Dan Stromberg <drsalists@gmail.com> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web