Discussion:
How compare hex numbers?
Erik Max Francis
2002-12-14 21:23:40 UTC
Permalink
Okay, so I just remove the line with the hex() function. Thank you for
a
good explanation!
And remove the quotes around your hexadecimal literal which make it a
string, not a number.
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ A physicist is an atom's way of knowing about atoms.
\__/ George Wald
Python chess module / http://www.alcyone.com/pyos/chess/
A chess game adjudicator in Python.
Ken Seehof
2002-12-15 01:17:09 UTC
Permalink
U = hex(U)
print "U is smaller."
print "U is bigger."
Python says U is bigger when U is in fact smaller. I've tried other ways to
What is the right syntax?
Gustaf
I recommend that you spend a bunch of time experimenting
in the python shell. You need to develop a clear intuitive
distinction between strings and integers in particular.

There is a funny quirk in python: objects of different types can
be compared (in most languages it's an error). It turns out that
there are good reasons for this, which I won't get into here (hint:
it has to do with sorting lists). It can be confusing though. In
particular, strings and integers can be compared, and the result
will not depend on the values! In fact strings are always greater
than integers, though this is not defined in the language spec,
so in some other python implementation, integers could be
always greater than strings.

Try the following expressions and see if they give the results
you expect. Continue playing with it until you can consistently
predict all of the results in advance. Note that boolean expressions
return 0 or 1, so you don't need to use "if" to test them.

'12' > '2'
int('A', 16)
int('0xA', 16)
hex(10)
int('0xA', 16) == 10
hex(10) == 16
1=='1'
9999<'1'
1<'99999'

- Ken
Martin v. Löwis
2002-12-14 12:03:55 UTC
Permalink
I'm trying to compare one hex number with another to see which is
bigger:

There are no hexadecimal integers in Python; the only kinds of integers
is the "plain" integer, and the "long" integer. The integers are objects
that support arithmethic operations (+,-,*,/,<,>, etc). They have
literals, which denote certain integers; those literals can be written
with decimal, octal, and hexadecimal digits.

And now for something completely different: the string object. Strings
do not support arithmetic operations, they only support concatenation
(+), and lexicographical comparison (<, >).

Then, there are conversion functions. hex() has an integer argument
(either short or long), and returns a string object, which is a
hexadecimal *representation* of the number, not the number itself - the
result is a string is a string is a string.
U = hex(U)
If U was an integer before, it is now a string.
Comparing two strings uses lexicographical comparison: '0x2' > '0x10',
just like 'Gustaf' < 'Martin'.
Python says U is bigger when U is in fact smaller. I've tried other
ways to
Now, if U is still a string, you are comparing strings and integers
here. Try guessing what the outcome *should* be, i.e. is 42 smaller or
greater than 'Gustaf'?
What is the right syntax?
To compare numbers, you should make sure that U is an integer, not a
string. Then you can compare it to another integer, which you might
denote through a hexadecimal *literal*, e.g. 0x10000.

HTH,
Martin
Gustaf Liljegren
2002-12-14 12:37:11 UTC
Permalink
Martin v. L?wis <martin at v.loewis.de> wrote in news:atf6n6$1uh$03$1 at news.t-
Post by Martin v. Löwis
To compare numbers, you should make sure that U is an integer, not a
string. Then you can compare it to another integer, which you might
denote through a hexadecimal *literal*, e.g. 0x10000.
Okay, so I just remove the line with the hex() function. Thank you for a
good explanation!

Gustaf
Andreas Jung
2002-12-14 11:49:37 UTC
Permalink
--On Samstag, 14. Dezember 2002 11:41 +0000 Gustaf Liljegren
U = hex(U)
'0x10000' is *NOT* a hex number. It is just a string.
You should compare it:

if U < 0x10000:......


---------------------------------------------------------------------
- Andreas Jung http://www.andreas-jung.com -
- EMail: andreas at andreas-jung.com -
- "Life is too short to (re)write parsers" -
---------------------------------------------------------------------
Erik Max Francis
2002-12-14 21:22:53 UTC
Permalink
Python says U is bigger when U is in fact smaller. I've tried other
ways to
What is the right syntax?
If U is in fact a number, then the first is the proper syntax. The
second is comparing it against a very confused four-character string,
and the third will result in a TypeError (hex is for converting numbers
to a hexadecimal string notation, it doesn't work with strings
themselves).
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ A physicist is an atom's way of knowing about atoms.
\__/ George Wald
Python chess module / http://www.alcyone.com/pyos/chess/
A chess game adjudicator in Python.
Gustaf Liljegren
2002-12-14 11:41:39 UTC
Permalink
I'm trying to compare one hex number with another to see which is bigger:

U = hex(U)
if U < '0x10000':
print "U is smaller."
else:
print "U is bigger."

Python says U is bigger when U is in fact smaller. I've tried other ways to
specify that '0x10000' is hexadecimal too, including:

if U < 0x10000:
if U < '\x10000':
if U < hex('0x10000'):

What is the right syntax?

Gustaf

Loading...