BCD List to HEX List

John Machin sjmachin at lexicon.net
Sun Jul 30 17:48:34 EDT 2006


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




More information about the Python-list mailing list