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. int('7d', 16)
125 int('19', 16)
25
---------------------------
The problem is negative values. If the unit returns the hex value 'e7',
it means -25, but python says it's 231:
--------------------------- int('e7', 16)
231
---------------------------
Does anyone have a clue a to what I need to do?
Thanks!
Andreas Lydersen 7 13990
On 15/06/2006 9:09 AM, an**************@gmail.com wrote: 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. int('7d', 16) 125 int('19', 16) 25 ---------------------------
The problem is negative values. If the unit returns the hex value 'e7', it means -25, but python says it's 231: --------------------------- int('e7', 16)
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 an**************@gmail.com wrote: The problem is negative values. If the unit returns the hex value 'e7', it means -25, but python says it's 231: --------------------------- int('e7', 16) 231 ---------------------------
Does anyone have a clue a to what I need to do?
def u2(x):
if x & 0x80: # MSB set -> neg.
return -((~x & 0xff) + 1)
else:
return x u2(int('e7', 16))
-25 u2(int('12', 16))
18
w.
On 15/06/2006 9:24 AM, Wojciech Muła wrote: an**************@gmail.com wrote: The problem is negative values. If the unit returns the hex value 'e7', it means -25, but python says it's 231: ---------------------------> int('e7', 16) 231 ---------------------------
Does anyone have a clue a to what I need to do?
def u2(x): if x & 0x80: # MSB set -> neg. return -((~x & 0xff) + 1)
Holy obfuscation, Batman!
else: return x
u2(int('e7', 16)) -25 u2(int('12', 16))
18
w. an**************@gmail.com writes: 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. value_from_reading('00')
0 value_from_reading('05')
5 value_from_reading('7f')
127 value_from_reading('80')
-128 value_from_reading('e7')
-25 value_from_reading('ff')
-1 value_from_reading('100')
-128 value_from_reading('fff')
-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.
--
\ "Remorse: Regret that one waited so long to do it." -- Henry |
`\ L. Mencken |
_o__) |
Ben Finney an**************@gmail.com wrote: 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.
int('7d', 16) 125 int('19', 16) 25
---------------------------
The problem is negative values. If the unit returns the hex value 'e7', it means -25, but python says it's 231: --------------------------- int('e7', 16)
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/
On 15/06/2006 10:31 AM, Ben Finney wrote: an**************@gmail.com writes:
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::
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. >>> value_from_reading('00') 0 >>> value_from_reading('05') 5 >>> value_from_reading('7f') 127 >>> value_from_reading('80') -128 >>> value_from_reading('e7') -25 >>> value_from_reading('ff') -1
Looks like TWOS complement to me. >>> value_from_reading('100') -128
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. >>> value_from_reading('fff')
-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.
John Machin <sj******@lexicon.net> writes: On 15/06/2006 10:31 AM, Ben Finney wrote: 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: pj |
last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter
whether they are truly positive or negative ?
I am having to cast each column to varchar to find out if there are
any...
|
by: Subrahmanyam Arya |
last post by:
Hi Folks ,
I am trying to solve the problem of reading the numbers correctly
from a serial line into an intel pentium processor machine .
I am reading 1 byte and 2byte data both positive...
|
by: illegal.prime |
last post by:
So I see from the documentation here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp
That the code uses the...
|
by: jaks.maths |
last post by:
How to convert negative integer to hexadecimal or octal number?
Ex: -568
What is the equivalent hexadecimal and octal number??
|
by: drtimhill |
last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.
For example, to get the last character in a string, I can enter "x". To get the...
|
by: Frederick Gotham |
last post by:
I have a general idea about how negative number systems work, but I'd
appreciate some clarification if anyone would be willing to help me.
Let's assume we're working with an 8-Bit signed integer,...
|
by: Ivan Novick |
last post by:
Hi,
Is it possible to have negative integer literal or only positive?
As far as I understand, the code below would be a positive integer
literal and the unary negative operator.
x = -3.2;
...
|
by: Hallvard B Furuseth |
last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is
negative zero. So for signed types, x == 0 does not guarantee
(x & foo) == 0. Is that right? (Not that I expect to ever
encounter a...
|
by: valentin tihomirov |
last post by:
My rateonale is:
-1 mod 3 = must be 3
0 mod 3 = 0
1 mod 3 = 1
2 mod 3 = 2
3 mod 3 = 3
4 mod 3 = 0
= 1
= 2
= 3
|
by: Casey |
last post by:
Is there an easy way to use getopt and still allow negative numbers as
args? I can easily write a workaround (pre-process the tail end of
the arguments, stripping off any non-options including...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |