Created
April 4, 2012 11:29
-
-
Save kontulai/2300468 to your computer and use it in GitHub Desktop.
tbcd decoding into one line
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
def to_tbcd(binary_string): | |
""" | |
tbcd decoding in one line | |
function takes argument in a form "0b10110101001" and translates it into a string | |
presentation of integer by tbcd standards | |
http://en.wikipedia.org/wiki/Binary-coded_decimal#Telephony_Binary_Coded_Decimal_.28TBCD.29 | |
""" | |
return "".join(str(int(binary_string[index: index + 4], 2)) if int(binary_string[index + 4:index + 8], 2) == 15 else "%s%s" % (int(binary_string[index + 4:index + 8], 2), int(binary_string[index: index + 4], 2)) for index in range(2, len(binary_string), 8)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment