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


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

problem with bcd and a number

Started bynephish <nephish@gmail.com>
First post2011-08-04 11:26 -0700
Last post2011-08-04 13:14 -0700
Articles 6 — 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:26 -0700
    Re: problem with bcd and a number Ethan Furman <ethan@stoneleaf.us> - 2011-08-04 12:44 -0700
    Re: problem with bcd and a number MRAB <python@mrabarnett.plus.com> - 2011-08-04 20:35 +0100
      Re: problem with bcd and a number Christoph Hansen <ch@radamanthys.de> - 2011-08-04 21:52 +0200
        Re: problem with bcd and a number Peter Pearson <ppearson@nowhere.invalid> - 2011-08-05 16:01 +0000
    Re: problem with bcd and a number Ethan Furman <ethan@stoneleaf.us> - 2011-08-04 13:14 -0700

#10867 — problem with bcd and a number

Fromnephish <nephish@gmail.com>
Date2011-08-04 11:26 -0700
Subjectproblem with bcd and a number
Message-ID<31c8d075-2901-46eb-adcf-1c1803ab7938@a4g2000yqg.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]


#10871

FromEthan Furman <ethan@stoneleaf.us>
Date2011-08-04 12:44 -0700
Message-ID<mailman.1900.1312486155.1164.python-list@python.org>
In reply to#10867
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.

As I recall, when you cut a byte in half you get two nibbles.  You need 
to translate your nibbles into numbers, then multiply and add -- 
something like this:

8<--- bcd.py ----------------------------------------------------------
def bcd_to_int(msb, lsb):
     """msb has two decimal digits, lsb has one
     e.g. msb has xy=10, lsb has zX = 00, actual number (xyz)
     is 100"""
     ones = lsb >> 4
     tens = msb & 0x0f
     hundreds = msb >> 4
     if ones > 9 or tens > 9 or hundreds > 9:
         raise ValueError(
               "invalid BCD digits in %02x %02x" % (msb, lsb)
               )
     tens *= 10
     hundreds *= 100
     answer = hundreds + tens + ones
     return answer

if __name__ == '__main__':
     import unittest

     class Test_BCD(unittest.TestCase):
         def test_valid(self):
             msb = 0x09
             lsb = 0x34  # 4 will be discarded as garbage
             self.assertEqual(bcd_to_int(msb, lsb), 93)
         def test_invalid(self):
             msb = 0x1a
             lsb = 0x10
             self.assertRaises(ValueError, bcd_to_int, msb, lsb)

     unittest.main()
8<---------------------------------------------------------------------

Hope this helps.

~Ethan~

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


#10874

FromMRAB <python@mrabarnett.plus.com>
Date2011-08-04 20:35 +0100
Message-ID<mailman.1901.1312486523.1164.python-list@python.org>
In reply to#10867
On 04/08/2011 19:26, 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.

The value is MSB * 100 + (LSB >> 4) * 10 + (LSB & 0xF)

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


#10876

FromChristoph Hansen <ch@radamanthys.de>
Date2011-08-04 21:52 +0200
Message-ID<j1etad$f5e$1@online.de>
In reply to#10874
MRAB schrieb:

> The value is MSB * 100 + (LSB>>  4) * 10 + (LSB&  0xF)

i would say

(MSB >> 4)*100 + (MSB & 0xF)*10 + (LSB >> 4)

but who knows

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


#10911

FromPeter Pearson <ppearson@nowhere.invalid>
Date2011-08-05 16:01 +0000
Message-ID<9a2ifcF1k7U1@mid.individual.net>
In reply to#10876
On Thu, 04 Aug 2011 21:52:45 +0200, Christoph Hansen <ch@radamanthys.de> wrote:
> MRAB schrieb:
>
>> The value is MSB * 100 + (LSB>>  4) * 10 + (LSB&  0xF)
>
> i would say
>
> (MSB >> 4)*100 + (MSB & 0xF)*10 + (LSB >> 4)
>
> but who knows

I concur.  I think the documentation is trying to say that the
low-order nibble of the LSB is garbage.

-- 
To email me, substitute nowhere->spamcop, invalid->net.

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


#10877

FromEthan Furman <ethan@stoneleaf.us>
Date2011-08-04 13:14 -0700
Message-ID<mailman.1902.1312487936.1164.python-list@python.org>
In reply to#10867
MRAB wrote:
> On 04/08/2011 19:26, 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.
> 
> The value is MSB * 100 + (LSB >> 4) * 10 + (LSB & 0xF)

Not according to the docs -- msb has two digits, lsb has one and garbage.

~Ethan~

[toc] | [prev] | [standalone]


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


csiph-web