Uuencoding is historically used to encode emails. Instructions for creating a Uuencoder are:
- Start with 3 bytes from the source, 24 bits in total.
- Split into 4 6-bit groupings, each representing a value in the range 0 to 63: bits (00-05), (06-11), (12-17) and (18-23).
- Add 32 to each of the values. With the addition of 32 this means that the possible results can be between 32 (" " space) and 95 ("_" underline). 96 ("`" grave accent) as the "special character" is a logical extension of this range.
- Output the ASCII equivalent of these numbers.
import platform
def dec_to_bin(int):
return bin(int).replace('0b', '')
def uuencode(string, filename):
if platform.system() == 'Windows':
mode = '644'
elif platform.system() == 'Linux':
mode = '744'
trail = 'begin ' + mode + ' ' + filename + '\n'
string_values = []
char_list = list(string)
bytes = ''
for char in char_list:
string_values.append(ord(char))
for ascii in string_values:
bytes += dec_to_bin(ascii)
three_byte = [bytes[p:p+24] for p in range(0, len(bytes), 24)]
if len(three_byte[-1]) < 24:
three_byte[-1] += (24 - len(three_byte[-1])) * '0'
four_six_bits = [three_byte[n][m:m+6] for n in range(len(three_byte)) for m in range(0, 24, 6)]
four_six_bits = [four_six_bits[k:k+4] for k in range(0, len(four_six_bits), 4)]
decimal_list = []
for x in range(len(four_six_bits)):
for z in range(4):
decimal_list.append(int(four_six_bits[x][z], 2))
for index, decimal in enumerate(decimal_list):
decimal_list[index] += 32
encoded_chars = [chr(decimal_list[o]) for o in range(len(decimal_list))]
length_char = chr(len(encoded_chars) + 32)
trail += length_char
for newchar in encoded_chars:
trail += newchar
trail += '\n' + '`' + '\n' + 'end' + '\n'
return trail