Last active
October 19, 2022 08:41
-
-
Save braddevans/102de81ca07f76c88f7d25034630924d to your computer and use it in GitHub Desktop.
binary to decimal in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
binaryStr = "101100101001" # binary: 2857 | |
decnumber = 0 | |
for index in range(0, len(binaryStr)): | |
# reverse string then process binary using calculation | |
# (binary_value * 2 ^ string_index) | |
decnumber += (int(binaryStr[::-1][index]) * (2 ** index)) | |
print(f"dec: {decnumber}") # 2857 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment