2

how do I convert Convert 32 bit binary to a decimal in python

this

00011110001101110110110000001000

to

506948616

this

2 Answers 2

2

use in-built function int():

a = '00011110001101110110110000001000'
a_dec = int(a, 2)
Sign up to request clarification or add additional context in comments.

1 Comment

This works great for positive numbers but not for negative ones where the leading digit is 1. You need additional work for that.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.