int given base 2 and then hex:
>>> int('010110', 2)
22
>>> hex(int('010110', 2))
'0x16'
>>>
>>> hex(int('0000010010001101', 2))
'0x48d'
The doc of int:
int(x[, base]) -> integer Convert a string or number to an integer, if possible. A floatingpoint argument will be truncated towards zero (this does not include a string representation of a floating point number!) When converting a string, use the optional base. It is an error to supply a base when converting a non-string. If base is zero, the proper base is guessed based on the string content. If the argument is outside the integer range a long object will be returned instead.
The doc of hex:
Answer from Eli Bendersky on Stack Overflowhex(number) -> string Return the hexadecimal representation of an integer or longinteger.
int given base 2 and then hex:
>>> int('010110', 2)
22
>>> hex(int('010110', 2))
'0x16'
>>>
>>> hex(int('0000010010001101', 2))
'0x48d'
The doc of int:
int(x[, base]) -> integer Convert a string or number to an integer, if possible. A floatingpoint argument will be truncated towards zero (this does not include a string representation of a floating point number!) When converting a string, use the optional base. It is an error to supply a base when converting a non-string. If base is zero, the proper base is guessed based on the string content. If the argument is outside the integer range a long object will be returned instead.
The doc of hex:
hex(number) -> string Return the hexadecimal representation of an integer or longinteger.
bstr = '0000 0100 1000 1101'.replace(' ', '')
hstr = '%0*X' % ((len(bstr) + 3) // 4, int(bstr, 2))
Python: binary/hex string conversion? - Stack Overflow
How can I convert a python binary string to hex? - Stack Overflow
How do I convert a string of bits to a hex string in Python? - Stack Overflow
Understanding how to convert binary to hexadecimal in python to control slave-chip
How can I convert binary to hex in Python?
Both approaches will be explained in detail in the respective sections of this guide.
Why is converting binary to hex in Python useful?
Hexadecimal numbers are also commonly used in various applications, such as network protocols and memory addresses.
Are there any built-in functions for converting binary to hex in Python?
This function simplifies the conversion process and is recommended for its convenience.
You can use ord and hex like this :
>>> s = 'some string'
>>> hex_chars = map(hex,map(ord,s))
>>> print hex_chars
['0x73', '0x6f', '0x6d', '0x65', '0x20', '0x73', '0x74', '0x72', '0x69', '0x6e', '0x67']
>>> hex_string = "".join(c[2:4] for c in hex_chars)
>>> print hex_string
736f6d6520737472696e67
>>>
Or use the builtin encoding :
>>> s = 'some string'
>>> print s.encode('hex_codec')
736f6d6520737472696e67
>>>
>>> import binascii
>>> s = '2F'
>>> hex_str = binascii.b2a_hex(s)
>>> hex_str
>>> '3246'
OR
>>>import binascii
>>> hex_str = binascii.hexlify(s)
>>> hex_str
>>> '3246'
>>>
To format to hexadecimal you can use the hex function:
>>> hex(int('10000011101000011010100010010111', 2))
0x83a1a897
Or to get it in exactly the format you requested:
>>> '%08X' % int('10000011101000011010100010010111', 2)
83A1A897
>>> binary = '10010111'
>>> int(binary,2)
151
>>> hex(int(binary,2))
'0x97'
I hope this helps!
Hi,
I'm not entirely sure this is the correct place to ask. I've a chip I am trying to control using the NI-8452. This is all based on an evaluation board and due to urgency I can't wait much for the supplier to respond. The evaluation board needs to receive a 48-bit message that can be then send in hexadecimal in python under the format 00NY::XX-XX-XX-XX-XX-XX where NY are the registers and XX would be hexadecimals. I only need to access the 23 last bits. I need to modify chunks of 6 bits and I calculate these 6 bits individually.
So from MSB:LSB I only need to access 23:0 in chunks of 5:0, those are power levels that I can calculate easily by doing round(ThereticalPower/MyDiscreteLevel). What I am struggling (and I'm pretty sure it is easy) is how to generate the hexadecimals for 48 bits (so the 12 hexa values) when changing this, so for instance I know if I have 11:6 == 63 and 5:0 == 63 I need to have 00-00-00-00-0f-ff. But I have no idea how to get there.
EDIT: I managed to do it by filling the binary
binary = bin(decimal)[2:] # Convert decimal to binary string binary = binary.zfill(num_bits)
Then I add the multiple 6-bit binaries and convert using the function described here