-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_c.py
37 lines (30 loc) · 1.24 KB
/
convert_to_c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def read_binary_file(file_path):
with open(file_path, 'rb') as file:
return file.read()
def convert_to_hex(byte_data):
# Agrupamos los datos de 8 en 8
byte_data = [byte_data[i:i+8] for i in range(0, len(byte_data), 8)]
# Convertimos cada byte a su representación en hexadecimal
resu = []
for i in range(len(byte_data)):
byte = sum([byte_data[i][j] << j for j in range(8)])
# Añadimos el valor en hexadecimal a la lista resu
resu.append(f'0x{byte:02X}')
return resu
def write_hex_file(hex_data, output_path):
with open(output_path, 'w') as file:
# Separamos los valores por comas y ponemos un salto de linea cada 16 valores
file.write ('static const char font_8x8[] = {\n\t')
slice_size = 16
for i in range(0, len(hex_data), slice_size):
file.write(', '.join(hex_data[i:i+slice_size]))
file.write(',\n\t')
file.write('};')
def main(input_file, output_file):
binary_data = read_binary_file(input_file)
hex_data = convert_to_hex(binary_data)
write_hex_file(hex_data, output_file)
if __name__ == "__main__":
input_file = './font_8x8_ega.data'
output_file = './font_8x8.h'
main(input_file, output_file)