Skip to content

Instantly share code, notes, and snippets.

@thomasbhatia
Forked from kontulai/gist:2300468
Created January 14, 2013 21:55
Show Gist options
  • Save thomasbhatia/4533888 to your computer and use it in GitHub Desktop.
Save thomasbhatia/4533888 to your computer and use it in GitHub Desktop.
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