Negative hex to int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andreas.lydersen@gmail.com

    Negative hex to int

    Hi!

    While communicating with a monitoring unit, I get some hex values
    representing degrees celcius from its probes. The values can be
    something like '19' or '7d'. To convert it to int, I do the following:
    ---------------------------
    Python 2.4.2 (#1, Sep 28 2005, 10:25:47)
    [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> int('7d', 16)[/color][/color][/color]
    125[color=blue][color=green][color=darkred]
    >>> int('19', 16)[/color][/color][/color]
    25[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    ---------------------------

    The problem is negative values. If the unit returns the hex value 'e7',
    it means -25, but python says it's 231:
    ---------------------------[color=blue][color=green][color=darkred]
    >>> int('e7', 16)[/color][/color][/color]
    231
    ---------------------------

    Does anyone have a clue a to what I need to do?

    Thanks!

    Andreas Lydersen

  • John Machin

    #2
    Re: Negative hex to int

    On 15/06/2006 9:09 AM, andreas.lyderse n@gmail.com wrote:[color=blue]
    > Hi!
    >
    > While communicating with a monitoring unit, I get some hex values
    > representing degrees celcius from its probes. The values can be
    > something like '19' or '7d'. To convert it to int, I do the following:
    > ---------------------------
    > Python 2.4.2 (#1, Sep 28 2005, 10:25:47)
    > [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
    > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
    >>>> int('7d', 16)[/color][/color]
    > 125[color=green][color=darkred]
    >>>> int('19', 16)[/color][/color]
    > 25
    > ---------------------------
    >
    > The problem is negative values. If the unit returns the hex value 'e7',
    > it means -25, but python says it's 231:
    > ---------------------------[color=green][color=darkred]
    >>>> int('e7', 16)[/color][/color]
    > 231
    > ---------------------------
    >[/color]

    The Da Vinci code it aint :-)

    |>> readings = ['19', 'e7']
    |>> for reading in readings:
    .... intval = int(reading, 16)
    .... if intval >= 128:
    .... intval -= 256
    .... print intval
    ....
    25
    -25

    Comment

    • Wojciech Mu³a

      #3
      Re: Negative hex to int

      andreas.lyderse n@gmail.com wrote:[color=blue]
      > The problem is negative values. If the unit returns the hex value 'e7',
      > it means -25, but python says it's 231:
      > ---------------------------[color=green][color=darkred]
      >>>> int('e7', 16)[/color][/color]
      > 231
      > ---------------------------
      >
      > Does anyone have a clue a to what I need to do?[/color]

      def u2(x):
      if x & 0x80: # MSB set -> neg.
      return -((~x & 0xff) + 1)
      else:
      return x
      [color=blue][color=green][color=darkred]
      >>> u2(int('e7', 16))[/color][/color][/color]
      -25[color=blue][color=green][color=darkred]
      >>> u2(int('12', 16))[/color][/color][/color]
      18[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      w.

      Comment

      • John Machin

        #4
        Re: Negative hex to int

        On 15/06/2006 9:24 AM, Wojciech Muła wrote:[color=blue]
        > andreas.lyderse n@gmail.com wrote:[color=green]
        >> The problem is negative values. If the unit returns the hex value 'e7',
        >> it means -25, but python says it's 231:
        >> ---------------------------[color=darkred]
        >>>>> int('e7', 16)[/color]
        >> 231
        >> ---------------------------
        >>
        >> Does anyone have a clue a to what I need to do?[/color]
        >
        > def u2(x):
        > if x & 0x80: # MSB set -> neg.
        > return -((~x & 0xff) + 1)[/color]

        Holy obfuscation, Batman!
        [color=blue]
        > else:
        > return x
        >[color=green][color=darkred]
        >>>> u2(int('e7', 16))[/color][/color]
        > -25[color=green][color=darkred]
        >>>> u2(int('12', 16))[/color][/color]
        > 18
        >
        > w.[/color]

        Comment

        • Ben Finney

          #5
          Re: Negative hex to int

          andreas.lyderse n@gmail.com writes:
          [color=blue]
          > The problem is negative values. If the unit returns the hex value
          > 'e7', it means -25, but python says it's 231:[/color]

          Python is right. There is no "negative bit" in Python numbers, now
          that unification of 'long' and 'int' is complete; numbers can grow
          indefinitely large.

          If you want a special interpretation of the value, you'll have to
          calculate it.

          Example assuming you want a one's-complement interpretation: :

          def value_from_read ing(num):
          """ Gets the integer value from the reading string.
          num is a positive hexadecimal number as a string.
          Numbers greater than 0x7F are interpreted as negative,
          with magnitude greater than 0x7F being the negative value.
          Only the lowest 15 bits are used for the magnitude.
          [color=blue][color=green][color=darkred]
          >>> value_from_read ing('00')[/color][/color][/color]
          0[color=blue][color=green][color=darkred]
          >>> value_from_read ing('05')[/color][/color][/color]
          5[color=blue][color=green][color=darkred]
          >>> value_from_read ing('7f')[/color][/color][/color]
          127[color=blue][color=green][color=darkred]
          >>> value_from_read ing('80')[/color][/color][/color]
          -128[color=blue][color=green][color=darkred]
          >>> value_from_read ing('e7')[/color][/color][/color]
          -25[color=blue][color=green][color=darkred]
          >>> value_from_read ing('ff')[/color][/color][/color]
          -1[color=blue][color=green][color=darkred]
          >>> value_from_read ing('100')[/color][/color][/color]
          -128[color=blue][color=green][color=darkred]
          >>> value_from_read ing('fff')[/color][/color][/color]
          -1

          """
          num_base = 16
          negative_thresh old = 0x7F
          int_val = int(num, num_base)
          if int_val > negative_thresh old:
          magnitude = (int_val & negative_thresh old)
          int_val = -(1 + negative_thresh old - magnitude)
          return int_val

          Adjust for whatever algorithm you want to use. Consult a book of
          algorithms if you want a better implementation than my off-the-cuff
          brute-force approach.

          --
          \ "Remorse: Regret that one waited so long to do it." -- Henry |
          `\ L. Mencken |
          _o__) |
          Ben Finney

          Comment

          • James Stroud

            #6
            Re: Negative hex to int

            andreas.lyderse n@gmail.com wrote:[color=blue]
            > Hi!
            >
            > While communicating with a monitoring unit, I get some hex values
            > representing degrees celcius from its probes. The values can be
            > something like '19' or '7d'. To convert it to int, I do the following:
            > ---------------------------
            > Python 2.4.2 (#1, Sep 28 2005, 10:25:47)
            > [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
            > Type "help", "copyright" , "credits" or "license" for more information.
            >[color=green][color=darkred]
            >>>>int('7d', 16)[/color][/color]
            >
            > 125
            >[color=green][color=darkred]
            >>>>int('19', 16)[/color][/color]
            >
            > 25
            >
            > ---------------------------
            >
            > The problem is negative values. If the unit returns the hex value 'e7',
            > it means -25, but python says it's 231:
            > ---------------------------
            >[color=green][color=darkred]
            >>>>int('e7', 16)[/color][/color]
            >
            > 231
            > ---------------------------
            >
            > Does anyone have a clue a to what I need to do?
            >
            > Thanks!
            >
            > Andreas Lydersen
            >[/color]


            py> t = lambda x: int(x, 16) - ((int(x, 16) >> 7) * 256)
            py> t('e7')
            -25


            --
            James Stroud
            UCLA-DOE Institute for Genomics and Proteomics
            Box 951570
            Los Angeles, CA 90095


            Comment

            • John Machin

              #7
              Re: Negative hex to int

              On 15/06/2006 10:31 AM, Ben Finney wrote:[color=blue]
              > andreas.lyderse n@gmail.com writes:
              >[color=green]
              >> The problem is negative values. If the unit returns the hex value
              >> 'e7', it means -25, but python says it's 231:[/color]
              >
              > Python is right. There is no "negative bit" in Python numbers, now
              > that unification of 'long' and 'int' is complete; numbers can grow
              > indefinitely large.
              >
              > If you want a special interpretation of the value, you'll have to
              > calculate it.
              >
              > Example assuming you want a one's-complement interpretation: :[/color]

              Given that the OP had to ask the question at all, it is doubtful that he
              knows what "one's-complement" means. He may well not be alone -- see later.
              [color=blue]
              >
              > def value_from_read ing(num):
              > """ Gets the integer value from the reading string.
              > num is a positive hexadecimal number as a string.
              > Numbers greater than 0x7F are interpreted as negative,
              > with magnitude greater than 0x7F being the negative value.
              > Only the lowest 15 bits are used for the magnitude.[/color]

              thing & 0x7F looks like lowest 7 bits to me.
              [color=blue]
              >[color=green][color=darkred]
              > >>> value_from_read ing('00')[/color][/color]
              > 0[color=green][color=darkred]
              > >>> value_from_read ing('05')[/color][/color]
              > 5[color=green][color=darkred]
              > >>> value_from_read ing('7f')[/color][/color]
              > 127[color=green][color=darkred]
              > >>> value_from_read ing('80')[/color][/color]
              > -128[color=green][color=darkred]
              > >>> value_from_read ing('e7')[/color][/color]
              > -25[color=green][color=darkred]
              > >>> value_from_read ing('ff')[/color][/color]
              > -1[/color]

              Looks like TWOS complement to me.
              [color=blue][color=green][color=darkred]
              > >>> value_from_read ing('100')[/color][/color]
              > -128[/color]

              Same result as '80'?
              In any case the OP gave no indication that he was getting more than two
              hex digits. His desired interpretation of 'e7' as -25 strongly indicates
              that he's getting only 2 hex digits.
              [color=blue][color=green][color=darkred]
              > >>> value_from_read ing('fff')[/color][/color]
              > -1
              >
              > """
              > num_base = 16
              > negative_thresh old = 0x7F
              > int_val = int(num, num_base)
              > if int_val > negative_thresh old:
              > magnitude = (int_val & negative_thresh old)
              > int_val = -(1 + negative_thresh old - magnitude)
              > return int_val
              >
              > Adjust for whatever algorithm you want to use. Consult a book of
              > algorithms if you want a better implementation than my off-the-cuff
              > brute-force approach.[/color]



              Comment

              • Ben Finney

                #8
                Re: Negative hex to int

                John Machin <sjmachin@lexic on.net> writes:
                [color=blue]
                > On 15/06/2006 10:31 AM, Ben Finney wrote:[color=green]
                > > If you want a special interpretation of the value, you'll have to
                > > calculate it.
                > >
                > > Example assuming you want a one's-complement interpretation: :[/color]
                >
                > Given that the OP had to ask the question at all, it is doubtful
                > that he knows what "one's-complement" means. He may well not be
                > alone -- see later.[/color]

                You've pointed out something useful: a quick attempt (by me, in this
                case) to describe the algorithm isn't worth much, less so if it's
                already well-documented. Hopefully the original poster can consult a
                more authoritative reference on the topic. Fortunately, that was also
                one of my points :-)

                The implementation itself seems to do the job the OP asked. I hope
                it's useful in some form.

                --
                \ "If society were bound to invent technologies which could only |
                `\ be used entirely within the law, then we would still be sitting |
                _o__) in caves sucking our feet." -- Gene Kan, creator of Gnutella |
                Ben Finney

                Comment

                Working...