Negative hex to int

A

andreas.lydersen

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

The problem is negative values. If the unit returns the hex value 'e7',
it means -25, but python says it's 231:
---------------------------231
---------------------------

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

Thanks!

Andreas Lydersen
 
J

John Machin

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.25
---------------------------

The problem is negative values. If the unit returns the hex value 'e7',
it means -25, but python says it's 231:
---------------------------231

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
 
W

Wojciech =?iso-8859-2?Q?Mu=B3a?=

The problem is negative values. If the unit returns the hex value 'e7',
it means -25, but python says it's 231:

def u2(x):
if x & 0x80: # MSB set -> neg.
return -((~x & 0xff) + 1)
else:
return x

w.
 
B

Ben Finney

The problem is negative values. If the unit returns the hex value
'e7', it means -25, but python says it's 231:

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_reading(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.
-1

"""
num_base = 16
negative_threshold = 0x7F
int_val = int(num, num_base)
if int_val > negative_threshold:
magnitude = (int_val & negative_threshold)
int_val = -(1 + negative_threshold - 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.
 
J

James Stroud

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.

25

---------------------------

The problem is negative values. If the unit returns the hex value 'e7',
it means -25, but python says it's 231:
---------------------------

231
---------------------------

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

Thanks!

Andreas Lydersen


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

http://www.jamesstroud.com/
 
J

John Machin

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

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.
def value_from_reading(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.

thing & 0x7F looks like lowest 7 bits to me.

Looks like TWOS complement to me.

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.
 
B

Ben Finney

John Machin said:
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.

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.
 
D

Dennis Lee Bieber

Does anyone have a clue a to what I need to do?
You've no doubt had lots of response by now, but... Here's a little
thing that works with differing "word" sizes...
.... mask = int("1" * wordlength, 2)
.... if value > (mask >> 1):
.... return -((~value + 1) & mask)
.... else:
.... return value
....
# 256, being 9 bits, obviously becomes a 0 when trimmed to 8-LSB
Thanks!

Andreas Lydersen
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top