Converting hex string to hex integer [duplicate] - python

This question already has answers here:
How to convert hex string to hex number?
(8 answers)
Closed 7 years ago.
I'm having problems converting a hex string like this: 0x187c to a hex integer without loosing zeros, and converting to integer.
I need the exact hex integer to find USB devices. The hex integers correspond to a vendor and product id.
I am using the following code to find devices:
dev = usb.core.find(idVendor=0x187c, idProduct=0x0521)
The problem is that I need to enter those numbers from a graphical interface. However, once the user types them I get them back as a string, and the usb.core module doesn't find the device.
>>> type(0x187c)
<type 'int'>
>>> type("0x187c")
<type 'str'>
>>> type(hex(int("0x187c",16)))
<type 'str'>
Is there any way to retrieve this value that will work with usb.core?

There are no "hex integers". An integer is an integer. It isn't important for your computer if it was originally written in hex, octal, binary or decimal.
dev = usb.core.find(idVendor=int(vendor, 16), idProduct=int(product, 16))
will simply work.

Take off the call to hex..
>>> type(int('0x187c', 16))
<type 'int'>
>>> print 0x187c
6268
>>> print int('0x187c', 16)
6268
>>> print int('0x187c', 16) == 0x187c
True
If int('0x187c', 16) isn't working then the problem is elsewhere..

Related

How to compare user input to a randomly generated number? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
So I made a "Guess the Number" program with Python and tested it. Apparently, choosing the correct number still comes up as the incorrect else statement. How can I fix this?
As you can see the 3 I entered apparently isn't the same as the 3 my program came up:
You're comparing the return value of your call to input (the string in your variable usernum) with the return value of random.randint which is an integer in your variable EasyRN.
You'll need to convert either the integer into an string:
usernum = int(usernum)
Or the string into an integer:
EasyRN = str(EasyRN)
Afterwards, you can use == to compare them.
The result of input() is text (in Python, a str, short for the word "string" which is used in programming), while the output of random.randint() is a number (an int, short for "integer").
>>> type("3")
<class 'str'>
>>> type(3)
<class 'int'>
If you compare a str and an int they will never be equivalent, as it's an apples-to-oranges comparison.
Look at the int() function which converts a string to an integer.
>>> int("3")
3
>>> type(int("3"))
<class 'int'>

Convert hex string to usably hex

I want to convert a hex string I read from a file
"0xbffffe43" to a value written in little endian "\x43\xfe\xff\xbf".
I've tried using struct.pack but it requires a valid integer. Everytime I try to cast hex functions it will convert the 43. I need this for an assignment around memory exploits.
I have access to python 2.7
a = "0xbffffe43"
...
out = "\x43\xfe\xff\xbf"
Is what I want to achieve
You have a string in input. You can convert it to an integer using int and a base.
>>> a = "0xbffffe43"
>>> import struct
>>> out = struct.pack("<I",int(a,16))
>>> out
b'C\xfe\xff\xbf'
The b prefix is there because solution was tested with python 3. But it works as python 2 as well.
C is printed like this because python interprets printable characters. But
>>> b'C\xfe\xff\xbf' == b'\x43\xfe\xff\xbf'
True
see:
Convert hex string to int in Python
Convert a Python int into a big-endian string of bytes
You can try doing:
my_hex = 0xbffffe43
my_little_endian = my_hex.to_bytes(4, 'little')
print(my_little_endian)

uint32 vs uint64: What bases do I need for the 'int()' function to work properly

If I have two hex-strings and want to convert one to an 32-bit unsigned integer and the other to a 64-bit unsigned integer, what bases would I provide the int() function?
Well, python usually decides how much memory to allocate itself. See the following example:
>>> type(int('0x7fffffff', 16))
<type 'int'>
>>> type(int('0x80000000', 16))
<type 'long'>
Based on the size of the number, Python allocates the right amount of memory.
BUT if you use the method long() instead of int(), always 8 bytes will be allocated, no matter what the number is:
>>> type(long('0x7fffffff', 16))
<type 'long'>
>>> type(long('0x80000000', 16))
<type 'long'>
*Tested for Python 2.7 (not tested with 3.x)
So I goofed up and int() does not determine the size or sign of your hex string.
By definition, hex is 16. So you would put in your string with the hex base of 16
int('A1S31231', 16)
The issue between 32 bit and 64 bit was simply the size of the string put in as an argument.
By virtue of their size,
2 hex characters = 1 byte
So if I had a 64 bit int, it would be 8 bytes or a 16 character hex string.
If I had a 32 bit int, it would be 4 bytes or 8 character hex string.
Based off Duncan's answer: In order to make your result unsigned. You would need to take your result and & them with their proper mask.
If you're looking to go from hex to an uint32 you would do the aforementioned int() conversion and then
result & 0xffffffff
If you wanted to go from hex to uint64 you would do the aforementioned int() conversion and then
result & 0xffffffffffffffff

Make Fix Size of Hex Number Python

I want make my hex number have 2 bytes size. How I can do it? Example,if in C we can make %02f. How I make it in Python2.7?
Thankyou
I try to use like this : "{0:03x}".format(number) and works
You can format it in a similar way. Just use "%02x" % (the_number,)
In python the use of hexadecimals is not that common. It is therefore typically represented as either a string or int, ie:
print type(0x50),0x50
#returns <type 'int'> 80
print type(hex(80),hex(80)
#returns <type 'str'>, '0x50'
Knowing this you, could exploit this property by doing something along the line of:
myhex_int=0x5 #integer with value 5
myhex_str='%02d'%myhex_int #string '05',use '0x02d' if you prefer this
#to return it to an integer again:
myhex2=int(myhex_str,16) #set base to 16
#integer with value 5

Convert String that already in binary to binary python

I already have a list that contain some string (as output of other process) as,(example)
BitStream = ['011000111001', '100100111001', '100101100110', ...]
Now i need to get the actual value as binary, not as string
But when i try to change it to binary with
for bit in BitStream:
BitInteger = bin(bit)
But it give me error message as str object cannot be interpreted as an index
But when i try to make change it as int first, and i change it to binary,
for bit in BitStream:
BitInteger = int(bit)
BitIntegerBin = bin(BitInteger)
The binary value is not the actual value in the string. But the value of actual binary that treated as integer that changed to binary.
How do i get the actual value?
Its not clear but what i understand:
>>> a
'011000111001'
>>> bin(int(a,2))
'0b11000111001' # python valid binary format
>>> int(a,2) # integer
1593
for your code integer and binary:
>>> BitStream = ['011000111001', '100100111001', '100101100110']
>>> [ [int(x,2),bin(int(x,2))] for x in BitStream ]
[[1593, '0b11000111001'], [2361, '0b100100111001'], [2406, '0b100101100110']]

Categories

Resources