Skip to content

Instantly share code, notes, and snippets.

@braddevans
Last active October 19, 2022 08:41
Show Gist options
  • Save braddevans/102de81ca07f76c88f7d25034630924d to your computer and use it in GitHub Desktop.
Save braddevans/102de81ca07f76c88f7d25034630924d to your computer and use it in GitHub Desktop.
binary to decimal in python
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