Last active
August 22, 2016 04:05
-
-
Save mhtsai1010/945135edb8196463c385 to your computer and use it in GitHub Desktop.
Python: binary to decimal
This file contains 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
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
def bin2dec(binary_string): | |
""" Convert binary string to decimal number """ | |
decimal = 0 | |
while binary_string: | |
decimal = decimal * 2 + (ord(binary_string[0]) - ord('0')) | |
binary_string = binary_string[1:] | |
return decimal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment