decoder.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # %%
  2. import csv
  3. # %%
  4. with open("vettSpec.csv", 'r') as file1:
  5. reader = csv.DictReader(file1)
  6. vettSpec = [row for row in reader]
  7. # %%
  8. testChar = 'Ҙ'
  9. print( testChar.encode("utf-32-le") )
  10. print( int.from_bytes(testChar.encode("utf-32-le"), 'little', signed=False) )
  11. pre = next((el['unicode'] for el in vettSpec if int(el['intcode']) == int.from_bytes(testChar.encode("utf-32-le"), 'little', signed=False)), None)
  12. res = bytes.fromhex(pre)
  13. while len(res)<4:
  14. res=res+b'\x00'
  15. print('pre-Res:', pre)
  16. print('Res-bytes:', res)
  17. print('Res:', res.decode("utf-32-le") )
  18. # %%
  19. # mmm...
  20. print('n'.encode("utf-32-be").hex().lstrip('0').upper())
  21. post = next((el['intcode'] for el in vettSpec if el['unicode'] == 'n'.encode("utf-32-be").hex().lstrip('0').upper()), None)
  22. print(post)
  23. byteini = int.to_bytes(int(post), 4, byteorder='little')
  24. byteini.decode('utf-32-le')
  25. # %%
  26. testoProva = 'C:/Users/Kora/Desktop/ndg2.gat4/ndg2.gat4/itxt/10n'
  27. # %%
  28. with open(testoProva, 'rb') as fille:
  29. azz = fille.read()
  30. # %%
  31. azz
  32. # %%
  33. def decode(char):
  34. pre = next((el['unicode'] for el in vettSpec if int(el['intcode']) == int.from_bytes(char.encode("utf-32-le"), 'little', signed=False)), None)
  35. resBytes = bytes.fromhex(pre)
  36. while len(resBytes)<4:
  37. resBytes=resBytes+b'\x00'
  38. res = resBytes.decode("utf-32-le")
  39. return res
  40. def decodeFromBytes(charBytes):
  41. pre = next((el['unicode'] for el in vettSpec if int(el['intcode']) == int.from_bytes(charBytes, 'little', signed=False)), None)
  42. resBytes = bytes.fromhex(pre)
  43. while len(resBytes)<4:
  44. resBytes=resBytes+b'\x00'
  45. res = resBytes.decode("utf-32-le")
  46. return res
  47. # %%
  48. str1 = ""
  49. for ind, char in enumerate(azz[:100]):
  50. print(char)
  51. str1 = str1+decode(char)
  52. print(str1)
  53. # %%
  54. print( azz[:4] )
  55. int.from_bytes(azz[:4], 'little', signed=False)
  56. # %%
  57. def decode(char):
  58. pre = next((el['unicode'] for el in vettSpec if int(el['intcode']) == int.from_bytes(char.encode("utf-32-le"), 'little', signed=False)), None)
  59. resBytes = bytes.fromhex(pre)
  60. while len(resBytes)<4:
  61. resBytes=resBytes+b'\x00'
  62. res = resBytes.decode("utf-32-le")
  63. return res
  64. testChar = 'Ҙ'
  65. print( testChar.encode("utf-32-le") )
  66. print( int.from_bytes(testChar.encode("utf-32-le"), 'little', signed=False) )
  67. pre = next((el['unicode'] for el in vettSpec if int(el['intcode']) == int.from_bytes(testChar.encode("utf-32-le"), 'little', signed=False)), None)
  68. print(pre)
  69. resBytes = bytes.fromhex(pre)
  70. print(resBytes)
  71. while len(resBytes)<4:
  72. resBytes=resBytes+b'\x00'
  73. print(resBytes)
  74. res = resBytes.decode("utf-32-le")
  75. print(res)
  76. # %%