problem with bcd and a number

Discussion in 'Python' started by nephish, Aug 4, 2011.

  1. nephish

    nephish Guest

    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.
    nephish, Aug 4, 2011
    #1
    1. Advertising

  2. nephish

    MRAB Guest

    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)
    MRAB, Aug 4, 2011
    #2
    1. Advertising

  3. nephish

    Ethan Furman Guest

    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~
    Ethan Furman, Aug 4, 2011
    #3
  4. 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
    Christoph Hansen, Aug 4, 2011
    #4
  5. nephish

    Ethan Furman Guest

    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~
    Ethan Furman, Aug 4, 2011
    #5
  6. On Thu, 04 Aug 2011 21:52:45 +0200, Christoph Hansen <> 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.
    Peter Pearson, Aug 5, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glen Herrmannsfeldt

    Re: binary to BCD assistance

    Glen Herrmannsfeldt, Jul 30, 2003, in forum: VHDL
    Replies:
    4
    Views:
    7,048
  2. Paul Leventis

    Re: binary to BCD assistance

    Paul Leventis, Jul 31, 2003, in forum: VHDL
    Replies:
    4
    Views:
    4,167
    jeppe
    Mar 26, 2008
  3. bob
    Replies:
    6
    Views:
    26,816
    G. G. Ditzel
    Nov 25, 2003
  4. Manaryu

    Problem with bcd conversion

    Manaryu, Sep 6, 2010, in forum: VHDL
    Replies:
    1
    Views:
    888
    Manaryu
    Sep 10, 2010
  5. nephish

    problem with bcd and a number

    nephish, Aug 4, 2011, in forum: Python
    Replies:
    4
    Views:
    205
    Peter Otten
    Aug 5, 2011
Loading...

Share This Page