BCD List to HEX List

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philippe Martin

    BCD List to HEX List

    Hi,

    I'm looking for an algo that would convert a list such as:

    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
    l2 = func (l1)
    # l2 = [0x1, 0x2, 0xD, 0x6, 0x8, 0x7] #represents 0x12D687


    Regards,

    Philippe

  • Marc 'BlackJack' Rintsch

    #2
    Re: BCD List to HEX List

    In <5S8zg.1553$W93 .658@dukeread05 >, Philippe Martin wrote:
    I'm looking for an algo that would convert a list such as:
    >
    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
    l2 = func (l1)
    # l2 = [0x1, 0x2, 0xD, 0x6, 0x8, 0x7] #represents 0x12D687
    def func(x):
    result = list(x)
    result[2:4] = [0xd]
    result[-1], result[-2] = result[-2], result[-1]
    return result

    l1 = [1, 2, 3, 4, 6, 7, 8]
    l2 = func(l1)
    print l2

    And now please describe you problem a little better. ;-)

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Philippe Martin

      #3
      Re: BCD List to HEX List

      Marc 'BlackJack' Rintsch wrote:
      In <5S8zg.1553$W93 .658@dukeread05 >, Philippe Martin wrote:
      >
      >I'm looking for an algo that would convert a list such as:
      >>
      >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
      >l2 = func (l1)
      ># l2 = [0x1, 0x2, 0xD, 0x6, 0x8, 0x7] #represents 0x12D687
      >
      def func(x):
      result = list(x)
      result[2:4] = [0xd]
      result[-1], result[-2] = result[-2], result[-1]
      return result
      >
      l1 = [1, 2, 3, 4, 6, 7, 8]
      l2 = func(l1)
      print l2
      >
      And now please describe you problem a little better. ;-)
      >
      Ciao,
      Marc 'BlackJack' Rintsch
      I'll try.

      first of all python is not going to be used for my purpose (sigh)

      I have device A which holds a binary coded decimal array [N1,N2,....Nn]
      where the array represents a decimal number.

      In C: unsigned char dec[] = {1,2,3,4,5,6,7, 8};

      I need that array converted for device B into an array where each element
      represents the actual byte value.

      In C: the result would be unsigned char hex[] = {0x1,0x2,0xD,0x 6,0x8,0x7};

      I guess any pocket calculator goes through that process for dec/hex
      conversion.

      Hope that's clearer.

      Regards,

      Philippe



      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: BCD List to HEX List

        In <ti9zg.1555$W93 .1057@dukeread0 5>, Philippe Martin wrote:
        Marc 'BlackJack' Rintsch wrote:
        >
        >And now please describe you problem a little better. ;-)
        >
        I'll try.
        >
        first of all python is not going to be used for my purpose (sigh)
        >
        I have device A which holds a binary coded decimal array [N1,N2,....Nn]
        where the array represents a decimal number.
        >
        In C: unsigned char dec[] = {1,2,3,4,5,6,7, 8};
        >
        I need that array converted for device B into an array where each element
        represents the actual byte value.
        >
        In C: the result would be unsigned char hex[] = {0x1,0x2,0xD,0x 6,0x8,0x7};
        >
        I guess any pocket calculator goes through that process for dec/hex
        conversion.
        >
        Hope that's clearer.
        Not really. Maybe I'm missing something obvious but I don't see the link
        between your `dec` and `hex` values. 12345678 converted to hex is
        bc614e. Do you need such a conversion? And should the result really be
        one nibble (4 bit)/hex digit per array entry!?

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Philippe Martin

          #5
          Re: BCD List to HEX List

          Marc 'BlackJack' Rintsch wrote:
          In <ti9zg.1555$W93 .1057@dukeread0 5>, Philippe Martin wrote:
          >
          >Marc 'BlackJack' Rintsch wrote:
          >>
          >>And now please describe you problem a little better. ;-)
          >>
          >I'll try.
          >>
          >first of all python is not going to be used for my purpose (sigh)
          >>
          >I have device A which holds a binary coded decimal array [N1,N2,....Nn]
          >where the array represents a decimal number.
          >>
          >In C: unsigned char dec[] = {1,2,3,4,5,6,7, 8};
          >>
          >I need that array converted for device B into an array where each element
          >represents the actual byte value.
          >>
          >In C: the result would be unsigned char hex[] =
          >{0x1,0x2,0xD,0 x6,0x8,0x7};
          >>
          >I guess any pocket calculator goes through that process for dec/hex
          >conversion.
          >>
          >Hope that's clearer.
          >
          Not really. Maybe I'm missing something obvious but I don't see the link
          between your `dec` and `hex` values. 12345678 converted to hex is
          bc614e. Do you need such a conversion? And should the result really be
          one nibble (4 bit)/hex digit per array entry!?
          >
          Ciao,
          Marc 'BlackJack' Rintsch
          My apologies, I clearly made a mistake with my calculator, yes the resulting
          array I would need is [0xb,0xc,0x6,0x1 ,0x4,0xe]

          Regards,

          Philippe



          Comment

          • John Machin

            #6
            Re: BCD List to HEX List

            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:


            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

            Comment

            • Philippe Martin

              #7
              Re: BCD List to HEX List

              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:

              >
              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]


              Philippe

              Comment

              • Paul Rubin

                #8
                Re: BCD List to HEX List

                Philippe Martin <pmartin@snakec ard.comwrites:
                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
                This is untested, but should give you the idea:

                First, convert that list to a decimal digit string:

                s = ''.join(map(str , l1))

                Then convert the string to an integer:

                n = int(s) # base 10 is the default

                Then convert the integer to a hex digit string:

                h = '%X' % n

                Finally, convert the hex digit string to a list of integer values of the
                individual digits:

                vlist = [int(d, 16) for d in h]

                This is the list you want.

                If you prefer, You can do it all in one line:

                vlist = [int(d, 16) for d in ('%X' % int(''.join(map (str, l1))))]

                Comment

                • Philippe Martin

                  #9
                  Re: BCD List to HEX List

                  Paul Rubin wrote:
                  Philippe Martin <pmartin@snakec ard.comwrites:
                  >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
                  >
                  This is untested, but should give you the idea:
                  >
                  First, convert that list to a decimal digit string:
                  >
                  s = ''.join(map(str , l1))
                  >
                  Then convert the string to an integer:
                  >
                  n = int(s) # base 10 is the default
                  >
                  Then convert the integer to a hex digit string:
                  >
                  h = '%X' % n
                  >
                  Finally, convert the hex digit string to a list of integer values of the
                  individual digits:
                  >
                  vlist = [int(d, 16) for d in h]
                  >
                  This is the list you want.
                  >
                  If you prefer, You can do it all in one line:
                  >
                  vlist = [int(d, 16) for d in ('%X' % int(''.join(map (str, l1))))]
                  Thanks Paul,

                  I'm just using Python to prototype, so I cannot use any of these great
                  features of the language.

                  Regards,

                  Philippe

                  Comment

                  • John Machin

                    #10
                    Re: BCD List to HEX List

                    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:


                    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_in t(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(a nint & 0xF)
                    anint >>= 4
                    return result

                    def reverse_list(al ist):
                    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.reconstitut e_int([1,2,3,4,5,6,7,8])
                    >>num
                    12345678
                    >>result = bcd.make_hex(nu m)
                    >>result
                    [14, 4, 1, 6, 12, 11]
                    >>bcd.reverse_l ist(result)
                    >>result
                    [11, 12, 6, 1, 4, 14]
                    >>['0x%x' % digit for digit in result]
                    ['0xb', '0xc', '0x6', '0x1', '0x4', '0xe']
                    >>^Z
                    HTH,
                    John

                    Comment

                    • Philippe Martin

                      #11
                      Re: BCD List to HEX List

                      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:

                      >
                      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_in t(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(a nint & 0xF)
                      anint >>= 4
                      return result
                      >
                      def reverse_list(al ist):
                      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.reconstitut e_int([1,2,3,4,5,6,7,8])
                      >>>num
                      12345678
                      >>>result = bcd.make_hex(nu m)
                      >>>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



                      Comment

                      • Philippe Martin

                        #12
                        Re: BCD List to HEX List

                        Dennis Lee Bieber wrote:
                        On Sun, 30 Jul 2006 16:39:47 -0500, Philippe Martin
                        <pmartin@snakec ard.comdeclaime d the following in comp.lang.pytho n:
                        >
                        >>
                        >My apologies, I clearly made a mistake with my calculator, yes the
                        >resulting array I would need is [0xb,0xc,0x6,0x1 ,0x4,0xe]
                        >>
                        Take note that this is NOT a BCD form for "12345678". BCD (typically
                        packed) uses four bits per decimal digit. That would make "12345678" =>
                        0x12, 0x34, 0x56, 0x78 (ignoring matters of big/little end).
                        >
                        The binary representation of 12345678, in bytes, is 0xBC, 0x61, 0x4E
                        >
                        0xb, 0xc... is really 0x0B, 0x0C... 8-bits per byte, with MSB set to
                        0000.
                        >
                        Compare:
                        BCD 00010010 00110100 01010110 01111000
                        binary 10111100 01100001 01001110
                        your 00001011 00001100 00000110 00000001 00000100 00001110
                        --
                        Wulfraed Dennis Lee Bieber KD6MOG
                        wlfraed@ix.netc om.com wulfraed@bestia ria.com

                        (Bestiaria Support Staff: web-asst@bestiaria. com)
                        HTTP://www.bestiaria.com/
                        Yes I realized that after writing it.

                        Regards,

                        Philippe

                        Comment

                        • Paul Rubin

                          #13
                          Re: BCD List to HEX List

                          Philippe Martin <pmartin@snakec ard.comwrites:
                          I'm just using Python to prototype, so I cannot use any of these great
                          features of the language.
                          I think when writing a prototype, you should use whatever features you
                          want, except maybe at the upper levels of program organization. The
                          idea of prototyping is to get something done quickly, that you can use
                          for integration and user testing earlier in the development cycle. So
                          you should use whatever Python features are available to make
                          prototyping faster. You shouldn't expect the prototype code to
                          closely match the final C code, if that slows you down. If you want
                          code that closely resembles C code, you might as well write in C
                          directly.

                          Comment

                          • Philippe Martin

                            #14
                            Re: BCD List to HEX List

                            Paul Rubin wrote:
                            Philippe Martin <pmartin@snakec ard.comwrites:
                            >I'm just using Python to prototype, so I cannot use any of these great
                            >features of the language.
                            >
                            I think when writing a prototype, you should use whatever features you
                            want, except maybe at the upper levels of program organization. The
                            idea of prototyping is to get something done quickly, that you can use
                            for integration and user testing earlier in the development cycle. So
                            you should use whatever Python features are available to make
                            prototyping faster. You shouldn't expect the prototype code to
                            closely match the final C code, if that slows you down. If you want
                            code that closely resembles C code, you might as well write in C
                            directly.

                            Some truth in that.

                            Thanks,

                            Philippe

                            Comment

                            • John Machin

                              #15
                              Re: BCD List to HEX List

                              Philippe Martin wrote:
                              >
                              Thanks John, I do not have a long available on the device: stuck with 16
                              bits.
                              >
                              What does "available on the device" mean? Having a "long" is a property
                              of a C complier, not a device. What is the CPU in the device? What is
                              the C compiler you are using? N.B. Last time I looked, gcc would
                              generate code for just about any CPU since Babbage's. I would expect
                              *any* C compiler that is targetting a 16-bit CPU to provide a 32-bit
                              long and generate reasonably efficient code for most simple operations.
                              Is there no library of utility routines for *elementary* base
                              conversions such as you need? Is there no 32-bit-arithmetic-simulation
                              package?

                              Comment

                              Working...