Skip to content

Instantly share code, notes, and snippets.

@mhtsai1010
Last active August 22, 2016 04:05
Show Gist options
  • Save mhtsai1010/945135edb8196463c385 to your computer and use it in GitHub Desktop.
Save mhtsai1010/945135edb8196463c385 to your computer and use it in GitHub Desktop.
Python: binary to decimal
#!/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