but all you do is translate from the alphabet list (tern) to numeric value (data list) and vice versa but that ain't no trytes or tryte conversion as far as i can see, if that is what you want to do. perhaps search on github for python projects which already do this? otherwise going after this:
https://steemit.com/iota/@wiredcrypto/understanding-iota-trits-and-trytes
# we can hardcode it like this
iota_alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '9']
# or generate it somewhat like this
import string
iota_alphabet = list(string.ascii_uppercase)
iota_alphabet.append(9)
# we know it's 27 characters long, A-Z and a 9, so we could just hardcode 27
remainder = ascii_val % 27
# or get it's length
remainder = ascii_val % len(iota_alphabet)
tryte_val_two = (ascii_val - remainder) / 27
# or again getting the length
tryte_val_two = (ascii_val - remainder) / len(iota_alphabet)
# this gives us a float val, we need an int
# so we make it one
tryte_val_two = int((ascii_val - remainder) / len(iota_alphabet))
# could also be done when getting the char from the list like this
iota_alphabet[int(tryte_val_two)]
# now we have the two characters for the trye, or at least their index in the alphabet
# let's get this done
tryte = iota_alphabet[remainder] + iota_alphabet[tryte_val_two]
looks great, but doesn't help that much in this cluttered fashion, so let's put this together in a function
def char_to_tryte(char):
ascii_val = ord(char)
# tc1 should be an int, but for good measure we make sure it will be one
tc1 = int(ascii_val % len(iota_alphabet))
tc2 = int((ascii_val - tc1) / len(iota_alphabet))
tryte = iota_alphabet[tc1] + iota_alphabet[tc2]
return tryte
print(char_to_tryte('H'))
SC
# ascii_val -> 72
# tc1 -> 18
# tc2 -> 2
this is working and gives us the right numbers but the return value looks kinda wrong. we wanted RB
and got SC
. luckily we know python lists start at 0, not 1. so what we need to do is subtract 1 from our values somewhere. either directly while getting them or while using them as list indices
# as list indices:
def char_to_tryte(char):
ascii_val = ord(char)
# tc1 should be an int, but for good measure we make sure it will be one
tc1 = int(ascii_val % len(iota_alphabet))
tc2 = int((ascii_val - tc1) / len(iota_alphabet))
tryte = iota_alphabet[tc1 - 1] + iota_alphabet[tc2 - 1]
return tryte
# while getting the numbers
def char_to_tryte(char):
ascii_val = ord(char)
# tc1 should be an int, but for good measure we make sure it will be one
tc1 = int((ascii_val % len(iota_alphabet)) - 1)
tc2 = int(((ascii_val - tc1) / len(iota_alphabet)) - 1)
tryte = iota_alphabet[tc1] + iota_alphabet[tc2]
return tryte
print(char_to_tryte('H'))
# now gives us
RB
# when subtracting from list indices
# ascii_val -> 72
# tc1 -> 18
# tc2 -> 2
# when subtracting while getting our numbers
# ascii_val -> 72
# tc1 -> 17
# tc2 -> 1