Hey, it doesn't glitch
It does exactly what you ask
Here is the code:
#!/usr/bin/env python
# This program converts 8 bit DEC files to 4 bit
import sys
def fixHex(h):
res = h.replace("0x","").upper()
if len(res) == 1:
res = "0"+res
return res
if len(sys.argv) < 2:
print("python convert.py <filename>")
exit(-1)
try:
file = open(sys.argv[1], "rb")
except:
print(sys.argv[1] + " can not be opened!")
exit(-1)
data = file.read()
out = ""
try:
for i in range(len(data)):
if i%4 == 0:
part1 = (ord(data[i])>>4)<<4
part2 = ord(data[i+3])>>4
out += fixHex(hex(part1+part2))
except:
print("Warning, file size odd! Last byte ignored.")
print(out[:-1])
Example input data:
A1 A1 B2 B2 3C 3C 4D 4D (but in binary form)
Output:
AB34 (in string)
So, what is does is loop 4 bytes a time over all the data.
The first byte and the third byte is what I need as the second and fourth are just the same.
Then I do there operations each loop:
a = (ord(byte1)>>4)<<4
b = (ord(byte3)>>4)
output = a + b