how do I convert Convert 32 bit binary to a decimal in python
this
00011110001101110110110000001000
to
506948616
this
use in-built function int():
a = '00011110001101110110110000001000'
a_dec = int(a, 2)
use int for conversion of a string (just give the correct base as parameter):
int('00011110001101110110110000001000', 2)
Another way is to add 0b as a prefix for the number (same as 0x for hex values or 0o for octal values):
x=0b00011110001101110110110000001000
x will be an integer with decimal value of 506948616
Related
I can't quite find a solution for this.
Basically what I've done so far is created a string which represents the binary version of x amount of characters padded to show all 8 bits.
E.g. if x = 2 then I have 0101100110010001 so 8 digits in total. Now I have 2 strings of the same length which I want to XOR together, but python keeps thinking it's a string instead of binary. If I use bin() then it throws a wobbly thinking it's a string which it is. So if I cast to an int it then removes the leading 0's.
So I've already got the binary representation of what I'm after, I just need to let python know it's binary, any suggestions?
The current function I'm using to create my binary string is here
for i in origAsci:
origBin = origBin + '{0:08b}'.format(i)
Thanks in advance!
Use Python's int() function to convert the string to an integer. Use 2 for the base parameter since binary uses base 2:
binary_str = '10010110' # Binary string
num = int(binary_str, 2)
# Output: 150
Next, use the bin() function to convert the integer to binary:
binary_num = bin(num)
# Output: 0b10010110
I want to write a function that takes a string of Binary and returns the result as Decimal.
I've written some code but if I didn't know it needed to be a string of Binary instead of just the numbers. Is there a way to change the code so it takes the string? I don't want to turn the binary to float, I want to turn it into decimal.
#Binary to Decimal
def bi_to_dec(binary):
binary1 = binary
decimal, i, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
return decimal
I'm going to assume this is an exercise where you have to write the algorithm yourself. Otherwise, you should just use the built-in function int(binary, 2) to parse the string binary in base 2 as an int.
If binary is a string, then the parts of your code you need to change are those which get the individual bits from the string. You can iterate over the bits in the string by writing for bit in reversed(binary):. This gives you a variable bit which is the current bit as a string, and because you reversed the string you get the bits in order from least-significant to most-significant as your algorithm requires. From there, you can simply convert the bit to an int using dec = int(bit).
Is there difference between '0x604f' and b'\x60\x4f' ?
1.how to change '0x604f' into b'\x60\x4f' in python ?
2.how to change b'\x60\x4f' into '0x604f' in python ?
I am in python3.3 .
You can use int(s, 0) to convert a string of the form 0x... into an integer. The argument 0 in place of an integer base instructs int to convert according to the prefix: 0x for base 16, 0o for base 8, 0b for base 2, and no prefix for base 10.
Then, you can use struct.pack (from the struct module) to pack the integer into a bytestring.
Demonstration:
>>> struct.pack('>H', int('0x604f', 0))
b'`O'
>>> b'\x60\x4f'
b'`O'
And in reverse, use hex and struct.unpack:
>>> hex(struct.unpack('>H', b'\x60\x4f')[0])
'0x604f'
Note that the format >H means "big-endian, 16-bit number". You can use a different format string for a different number of bytes (B for an 8-bit number, I for a 32-bit number, Q for a 64-bit number).
Since Python 3.2 you can do it directly using int.to_bytes as:
>>> int('0x604f', 16).to_bytes(2, 'big')
b'`O'
>>>
and the other way around using int.from_bytes:
>>> int.from_bytes(b'\x60\x4f', 'big')
24655
>>>
How can i convert Hex Digits to Binary and then perform "OR" operation in Python 2.4.3 ?
Example-
a= '1000'
b= '2000'
//Now convert both numbers a and b to binary form
//a_inbinary= '0001000000000000'
//b_inbinary= '0010000000000000'
c= a_inbinary | b_inbinary // or of a and b
//c would be result example - 0011000000000000
result=3000 //c in hex
Can someone please tell me that how i can convert two numbers to binary form and then result into hexadecimal form ?
You dont have to convert them to binary form to do the bitwise operations, but decimal integers should be fine. You can convert the numbers to base 10 by specifying the actual base, in which the numbers are represented, as the second parameter to int function. Finally, the result which is in decimal form can be converted back to hexa decimal using, hex function.
a= '1000'
b= '2000'
print hex(int(a, 16) | int(b, 16))
Output
0x3000
Assuming the first two strings are 0x1000 and 0x2000 there is no need to convert to a binary string, just use the bitwise or operator |
a='1000'
b='2000'
a=int(a,16)
b=int(b,16)
hex(a|b)
I have the variable Number which is equal to "0b11001010" and I want it to be the type int like a normal binary is stored e.g. 0b11001010
Number = "0b11001010"
NewNumber = 0b11001010
is there a really simple way and I am overlooking it?
Thanks.
In python you can only create it as a binary value (as a syntactic sugar), it will be converted into an integer immediately. Try it for yourself:
>>> 0b11001010
202
The same thing will happen with octal and hexadecimal values. So you can convert your binary string to an integer, with the int() function's base argument like:
>>> int('0b11001010', 2)
202
After the conversion you can do any operations on it -- just like with an integer, since it is an integer.
Of course you can convert it back at any time to a binary string, with the builtin bin() function:
>>> bin(202)
0b11001010