1

I am working on a CPU emulator in Python, and I`m making a program to read ROM files.

The code that I have right now is:

ROMCHARS = []
ROMHEX = []
with open(RomPath, 'r') as f:
    ROM = f.readline(1)
    
ROMCHARS = list(ROM)
for i in range(0, len(ROMCHARS)):
    ROMHEX.append()
    ROMHEX[i] = [Here the Code for Converting the UTF-16 Charater to Hex]
return ROMHEX

It converts each UTF-16 character into a hex code, puts it into a list, and returns the list.

The problem is that I haven’t figured out how to convert the characters to hexadecimal.

I already tried:

number = int('0030', base=16)
print(str(number))

But that just outputs nothing.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 2
    Please add an example of the contents of `RomPath`. – Cow Aug 30 '22 at 07:47
  • 1
    I am not sure what you mean by hex of a character. What is the hex of 'a' for example? – Mortz Aug 30 '22 at 08:39
  • this question was asked and answered before here: https://stackoverflow.com/questions/6999737/convert-from-hex-character-to-unicode-character-in-python – D.L Aug 30 '22 at 09:46
  • Maybe `[f'{ord(cc):04X}' for cc in ROM]`? – JosefZ Aug 30 '22 at 12:46
  • `f.readline(1)` will read at max only 1 character from `RomPath` so that clearly isn't what you want. What is the real code you've tried and what is the expected vs. actual result? What is the content of the file `RomPath`? give an example. – Mark Tolonen Aug 30 '22 at 15:38

0 Answers0