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


Groups > comp.lang.python > #10871

Re: problem with bcd and a number

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'something,': 0.07; 'unittest': 0.07; '__name__': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'garbage': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'wrote:': 0.15; '"invalid': 0.16; "'__main__':": 0.16; 'discarded': 0.16; 'packet': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'def': 0.16; 'bytes': 0.19; 'subject:problem': 0.19; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'parse': 0.23; 'byte': 0.25; 'there.': 0.25; 'hey': 0.26; 'raise': 0.28; 'all,': 0.28; 'import': 0.29; 'second': 0.29; 'cc:addr:python.org': 0.30; 'port.': 0.30; 'subject:number': 0.30; 'tens': 0.30; 'thanks': 0.31; 'class': 0.31; 'translate': 0.31; 'this.': 0.31; 'header:User-Agent:1': 0.34; 'pull': 0.37; 'but': 0.37; 'subject:: ': 0.38; 'something': 0.38; 'two': 0.38; 'subject:with': 0.39; 'skip:s 20': 0.39; 'missing': 0.40; 'where': 0.40; 'hope': 0.60; 'your': 0.60; 'maximum': 0.62; 'radio': 0.63; 'received:websitewelcome.com': 0.65; 'received:184': 0.67; 'serial': 0.71; '100': 0.72; 'received:69.56': 0.73; '100%': 0.82; 'need,': 0.91
Date Thu, 04 Aug 2011 12:44:41 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version 1.0
To nephish <nephish@gmail.com>
Subject Re: problem with bcd and a number
References <31c8d075-2901-46eb-adcf-1c1803ab7938@a4g2000yqg.googlegroups.com>
In-Reply-To <31c8d075-2901-46eb-adcf-1c1803ab7938@a4g2000yqg.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:1530
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1900.1312486155.1164.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1312486155 news.xs4all.nl 23881 [2001:888:2000:d::a6]:58664
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10871

Show key headers only | View raw


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~

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web