Last active
September 20, 2022 17:51
-
-
Save nixeneko/b3ec5681ab55040f9cae23a46d491cc4 to your computer and use it in GitHub Desktop.
Convert DICT binary to human-readable key-value pairs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# coding: utf-8 | |
# Print human-readable key-value pairs. | |
# output: val_a1 val_a2 ... <key_a> val_b1 ... <key_b> | |
# input DICT data (space-separated hex string) | |
DICTSTR = "F8 1B F8 1C 8B 0C 1E F8 1D 01 F8 1E 02 F8 1F 03 F8 18 04 FB 2A 0C 03 FE 7E FE AC 1D 00 00 0B 70 1D 00 00 07 10 05 8C 96 1D 00 8D 83 C8 0E 1E 1A 00 4F 0C 1F 1D 00 00 FF FF 0C 22 1D 00 00 3C B6 0F 1D 00 00 3C BB 0C 25 1D 00 DC 32 6B 0C 24 1D 00 00 3E 13 11" | |
#DICTSTR = "F8 2E 0C 26 A1 1D 00 EB 78 6C 12" | |
#DICTSTR = "FB 8E 8B 1D 00 00 05 46 8B 06 D3 0A D5 0B 8C 0C 11 FA 7C 14 A1 13" | |
dict_data = list(map(lambda n: int(n, 16), DICTSTR.split())) | |
#print(dict_data) | |
def twos_comp(val, bits): | |
"""compute the 2's complement of int value val""" | |
# https://stackoverflow.com/questions/1604464/twos-complement-in-python | |
if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255 | |
val = val - (1 << bits) # compute negative value | |
return val | |
idx = 0 | |
while idx < len(dict_data): | |
b0 = dict_data[idx] | |
idx+=1 | |
# operands | |
if 32 <= b0 <= 246: | |
val = b0 - 139 | |
print(val, end=" ") | |
elif 247 <= b0 <= 250: | |
b1 = dict_data[idx] | |
idx+=1 #use b1 | |
val=(b0 - 247)*256+b1+108 | |
print(val, end=" ") | |
elif 251 <= b0 <= 254: | |
b1 = dict_data[idx] | |
idx+=1 #use b1 | |
val=-(b0 - 251)*256-b1-108 | |
print(val, end=" ") | |
elif b0 == 28: | |
b1 = dict_data[idx] | |
idx+=1 #use b1 | |
b2 = dict_data[idx] | |
idx+=1 #use b2 | |
val = twos_comp(b1<<8|b2, 16) #-32768 - +32767 | |
print(val, end=" ") | |
elif b0 == 29: | |
b1 = dict_data[idx] | |
idx+=1 #use b1 | |
b2 = dict_data[idx] | |
idx+=1 #use b2 | |
b3 = dict_data[idx] | |
idx+=1 #use b3 | |
b4 = dict_data[idx] | |
idx+=1 #use b4 | |
val = twos_comp(b1<<24|b2<<16|b3<<8|b4, 32) | |
print(val, end=" ") | |
elif b0 == 30: # real number operand | |
valstr = "" | |
def nib_conv(nib): | |
return ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "E", "E-", "<resv>", "-", "end_of_number"][nib] | |
while True: | |
nib = dict_data[idx] | |
idx+=1 | |
nib_m = nib >> 4 | |
nib_l = nib & 0b1111 | |
if nib_m == 0xF: | |
break | |
valstr += nib_conv(nib_m) | |
if nib_l == 0xF: | |
break | |
valstr += nib_conv(nib_l) | |
val = float(valstr) | |
print(valstr, end=" ") | |
# operators | |
elif b0 == 12: | |
b1 = dict_data[idx] | |
idx+=1 #use b1 | |
print("<{} {}>".format(b0,b1)) | |
elif 0<= b0 <= 21: | |
print("<{}>".format(b0)) | |
else: | |
assert False, "an unknown operator or operand" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment