Skip to content

Instantly share code, notes, and snippets.

View kontulai's full-sized avatar

Ilmari Kontulainen kontulai

  • Helsinki Finland
View GitHub Profile
Verifying my identity on Peepeth.com 0x1e7bcfbd53fdfb37541726a96fe97fac35e29ac6
@kontulai
kontulai / gist:2300468
Created April 4, 2012 11:29
tbcd decoding into one line
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))