How can I perform a conversion of a binary string to the corresponding hex value in Python?
I have 0000 0100 1000 1101 and I want to get 048D I'm using Python 2.6.
How can I perform a conversion of a binary string to the corresponding hex value in Python?
I have 0000 0100 1000 1101 and I want to get 048D I'm using Python 2.6.
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))
Converting Binary into hex without ignoring leading zeros:
You could use the format() built-in function like this:
"{0:0>4X}".format(int("0000010010001101", 2))
On python3 using the hexlify function:
import binascii
def bin2hex(str1):
bytes_str = bytes(str1, 'utf-8')
return binascii.hexlify(bytes_str)
a="abc123"
c=bin2hex(a)
c
Will give you back:
b'616263313233'
and you can get the string of it like:
c.decode('utf-8')
gives:
'616263313233'
I would do:
dec_str = format(int('0000010010001101', 2),'x')
dec_str.rjust(4,'0')
Result: '048d'
To convert binary string to hexadecimal string, we don't need any external libraries. Use formatted string literals (known as f-strings). This feature was added in python 3.6 (PEP 498)
>>> bs = '0000010010001101'
>>> hexs = f'{int(bs, 2):X}'
>>> print(hexs)
>>> '48D'
If you want hexadecimal strings in small-case, use small "x" as follows
f'{int(bs, 2):x}'
Where bs inside f-string is a variable which contains binary strings assigned prior
f-strings are lost more useful and effective. They are not being used at their full potential.
Assuming they are grouped by 4 and separated by whitespace. This preserves the leading 0.
b = '0000 0100 1000 1101'
h = ''.join(hex(int(a, 2))[2:] for a in b.split())
>>> import string
>>> s="0000 0100 1000 1101"
>>> ''.join([ "%x"%string.atoi(bin,2) for bin in s.split() ] )
'048d'
>>>
or
>>> s="0000 0100 1000 1101"
>>> hex(string.atoi(s.replace(" ",""),2))
'0x48d'
x = int(input("press 1 for dec to oct,bin,hex \n press 2 for bin to dec,hex,oct \n press 3 for oct to bin,hex,dec \n press 4 for hex to bin,dec,oct \n"))
if x is 1:
decimal =int(input('Enter the decimal number: '))
print(bin(decimal),"in binary.")
print(oct(decimal),"in octal.")
print(hex(decimal),"in hexadecimal.")
if x is 2:
binary = input("Enter number in Binary Format: ");
decimal = int(binary, 2);
print(binary,"in Decimal =",decimal);
print(binary,"in Hexadecimal =",hex(decimal));
print(binary,"in octal =",oct(decimal));
if x is 3:
octal = input("Enter number in Octal Format: ");
decimal = int(octal, 8);
print(octal,"in Decimal =",decimal);
print(octal,"in Hexadecimal =",hex(decimal));
print(octal,"in Binary =",bin(decimal));
if x is 4:
hex = input("Enter number in hexa-decimal Format: ");
decimal = int(hex, 16);
print(hex,"in Decimal =",decimal);
print(hex,"in octal =",oct(decimal));
print(hex,"in Binary =",bin(decimal));