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

    Dave Angel Guest

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

  3. 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
     
    Christoph Hansen, Aug 4, 2011
    #3
  4. On Fri, Aug 5, 2011 at 1:40 AM, Dan Stromberg <> 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
     
    Chris Angelico, Aug 5, 2011
    #4
  5. nephish

    Peter Otten Guest

    Chris Angelico wrote:

    > On Fri, Aug 5, 2011 at 1:40 AM, Dan Stromberg <> 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.
     
    Peter Otten, Aug 5, 2011
    #5
    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,064
  2. Paul Leventis

    Re: binary to BCD assistance

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

    Problem with bcd conversion

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

    problem with bcd and a number

    nephish, Aug 4, 2011, in forum: Python
    Replies:
    5
    Views:
    280
    Peter Pearson
    Aug 5, 2011
Loading...

Share This Page