Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python hexadecimal comparison

Tags:

python

hex

I got a problem I was hoping someone could help me figure out!

I have a string with a hexadecimal number = '0x00000000' which means:

0x01000000 = apple  
0x00010000 = orange  
0x00000100 = banana   

All combinations with those are possible. i.e., 0x01010000 = apple & orange

How can I from my string determine what fruit it is? I made a dictionary with all the combinations and then comparing to that, and it works! But I am wondering about a nicer way of doing it.

like image 928
heffaklump Avatar asked Dec 11 '09 13:12

heffaklump


People also ask

How do you find the hexadecimal equivalent of a number in Python?

Python hex() FunctionThe hex() function converts the specified number into a hexadecimal value. The returned string always starts with the prefix 0x .

How to use hex() in Python?

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.

How does Python compare binary values?

Algorithm. Step 1 : Given two numbers. Step 2 : Convert both number into its binary using bin() function and remove first two characters because of bin(). Step 3 : Since binary representation of both numbers could differ in length so we will append zeroes in start of shorter string to make both string of equal length.

How to generate hexadecimal numbers in Python?

hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.


2 Answers

Convert your string to an integer, by using the int() built-in function and specifying a base:

>>> int('0x01010000',16)
16842752

Now, you have a standard integer representing a bitset. use &, | and any other bitwise operator to test individual bits.

>>> value  = int('0x01010000',16)
>>> apple  = 0x01000000
>>> orange = 0x00010000
>>> banana = 0x00000100
>>> bool(value & apple) # tests if apple is part of the value
True
>>> value |= banana     # adds the banana flag to the value
>>> value &= ~orange    # removes the orange flag from the value

Now, if you need to convert back to your string:

>>> hex(value)
'0x1000100'
like image 120
Adrien Plisson Avatar answered Sep 28 '22 06:09

Adrien Plisson


You could first of all convert your string to an integer:

s = "0x01010000"
i = int(s, 16) #i = 269484032

then, you could set up a list for the fruits:

fruits = [(0x01000000, "apple"), (0x00010000, "orange"), (0x00000100, "banana")]

for determing what fruits you have that is enough:

s = "0x01010000"
i = int(s, 16)
for fid,fname in fruits:
    if i&fid>0:
        print "The fruit '%s' is contained in '%s'" % (fname, s)

The output here is:

The fruit 'apple' is contained in '0x01010000'
The fruit 'orange' is contained in '0x01010000'
like image 24
Johannes Weiss Avatar answered Sep 28 '22 06:09

Johannes Weiss