Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Encoding ASCII text as DNA (quaternary base)
def to_base(n, bse):
digs = "0123456789abcdefghijklmnopqrstuvwxyz"
tmp = []
while n:
n, i = divmod(n, bse)
tmp.append(digs[i])
return "".join(tmp[::-1])
def chng_frm_base(s, frm_bse, to_bse):
if to_bse < 2 or to_bse > 36 or frm_bse < 2 or frm_bse > 36:
raise ValueError("bases must be between 2-36")
try:
return to_base(int(s, frm_bse), to_bse)
except ValueError:
try:
n = int("".join([ch for ch in s if ch.isdigit()]), frm_bse)
return to_base(n, to_bse)
except ValueError:
return 0
def to_dna_bases(str):
bin_ascii = bin(int.from_bytes(str.encode(), 'big'))[2:]
new_base = chng_frm_base(bin_ascii, 2, 4)
char_replaced = new_base.replace("0", "A")
char_replaced = char_replaced.replace("1", "T")
char_replaced = char_replaced.replace("2", "G")
char_replaced = char_replaced.replace("3", "C")
return char_replaced
def from_dna_bases(string):
char_replaced = string.replace("A", "0")
char_replaced = char_replaced.replace("T", "1")
char_replaced = char_replaced.replace("G", "2")
char_replaced = char_replaced.replace("C", "3")
bin_ascii = chng_frm_base(char_replaced, 4, 2)
n = int(bin_ascii, 2)
final = n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()
return final
dna_string = to_dna_bases("hello, world")
real_string = from_dna_bases(dna_string)
print(dna_string + " means `" + real_string + "`")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.