BCD List to HEX List

Philippe Martin pmartin at snakecard.com
Sun Jul 30 18:29:57 EDT 2006


John Machin wrote:

> Philippe Martin wrote:
>> John Machin wrote:
>>
>> > Philippe Martin wrote:
>> >> Hi,
>> >>
>> >> I'm looking for an algo that would convert a list such as:
>> >
>> > Such as what?
>> >
>> >>
>> >> I'm using python to prototype the algo: this will move to C in an
>> >> embedded system where an int has 16 bits - I do not wish to use any
>> >> python library.
>> >>
>> >> l1 = [1,2,3,4,6,7,8] #represents the decimal number 12345678
>> >
>> > Does it??? How do you represent the decimal number 12349678, then?
>> >
>> >> l2 = func (l1)
>> >> # l2 = [0x1, 0x2, 0xD, 0x6, 0x8, 0x7] #represents 0x12D687
>> >>
>> >
>> > I'm sorry, but very little of that makes any sense to me:
>> >
>> > 1. I thought BCD meant something very much like this:
>> > http://en.wikipedia.org/wiki/Binary-coded_decimal
>> >
>> > 2. >>> [0x1, 0x2, 0xD, 0x6, 0x8, 0x7] #represents 0x12D687
>> > [1, 2, 13, 6, 8, 7]
>> >
>> > So [1], [2], [6] are unchanged, [3, 4] -> [13] (or maybe [3, 4, 5] ->
>> > 13),  and [7, 8] -> [8,7].
>> >
>> > I doubt very much that there's an algorithm to do that. What is the
>> > relationship between 1234(maybe 5)678 and 0x12D687??? I would expect
>> > something like this::
>> >
>> >     0x12345678 (stored in 4 bytes 0x12, ..., 0x78)  -- or 0x21436587
>> > or
>> >     0x012345678s (where s is a "sign" nibble; stored in 5 bytes 0x01,
>> > ..., 0x8s)
>> >
>> > IOW something regular and explicable ...
>> >
>> > 3. Perhaps it might be a good idea if you told us what the *real*
>> > problem is, including *exact* quotes from the manual for the embedded
>> > system. You evidently need/want to convert from one representation of
>> > signed? unsigned? integers to another. Once we all understand *what*
>> > those representations are, *then* we can undoubtedly help you with
>> > pseudocode in the form of Python code manipulating lists or whatever.
>> >
>> > Cheers,
>> > John
>>
>>
>> Hi,
>>
>> From my answer to Marc:
>>
>> >My apologies, I clearly made a mistake with my calculator, yes the
>> >resulting
>> >array I would need is [0xb,0xc,0x6,0x1,0x4,0xe]
>>
> 
> "Clearly"? I don't think that word means what you think it means :-)
> 
> All you need is something like the following. You will need to use
> "long" if the C "int" is only 16 bits.
> 
> C:\junk>type bcd.py
> def reconstitute_int(alist):
>     reg = 0 # reg needs to be 32-bits (or more)
>     for digit in alist:
>         assert 0 <= digit <= 9
>         reg = reg * 10 + digit
>     return reg
> 
> def make_hex(anint):
>     # anint needs to be 32-bits (or more)
>     result = []
>     while anint:
>         result.append(anint & 0xF)
>         anint >>= 4
>     return result
> 
> def reverse_list(alist):
>     n = len(alist)
>     for i in xrange(n >> 1):
>         reg1 = alist[n - 1 - i]
>         reg2 = alist[i]
>         alist[i] = reg1
>         alist[n - 1 - i] = reg2
> 
> C:\junk>
> C:\junk>python
> Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
> on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import bcd
>>>> num = bcd.reconstitute_int([1,2,3,4,5,6,7,8])
>>>> num
> 12345678
>>>> result = bcd.make_hex(num)
>>>> result
> [14, 4, 1, 6, 12, 11]
>>>> bcd.reverse_list(result)
>>>> result
> [11, 12, 6, 1, 4, 14]
>>>> ['0x%x' % digit for digit in result]
> ['0xb', '0xc', '0x6', '0x1', '0x4', '0xe']
>>>> ^Z
> 
> HTH,
> John

Thanks John, I do not have a long available on the device: stuck with 16
bits.

Regards,

Philippe






More information about the Python-list mailing list