Search code examples
matlabencodingbinaryreverse-engineering

Matlab open a file in hexadecimal bit after bit


I want to collect infos from a proprietary type file. I have the software on my PC to test how it is coded. When you convert it to txt, I can locate where my infos are written in the file.

If I put 1->803F, 2->0040, 3->4040, 4->8040, 5->A040 , 6->C040, 7->E040, 8->0041, 9->1041. My number are float.

If I load them in matlab or convert to a text file, I have the sequence €? @ à@ A A  @ À@ @@ €@ instead of having m?m@à@mAmA@À@@@m@. I lost some information... (m are the missing or different character)

I want to have a matlab script to get these data in the hexadecimal way and not converted into text has some characters are not compatible.

Is it possible ?


Solution

  • I'm not sure what it actually is that you want but reading your file as a byte array opens all opportunities, e.g. print its content as hex digits:

    with open("file.txt", "rb") as f:
        content = f.read()
    
    print("Hex:", content.hex())
    
    # print each byte in different forms (char, int, hex code, two-digit hex code)
    for i, c  in enumerate(content):
        print("{}({}): {} = {}".format(chr(c), c, hex(c), content.hex()[2*i:2*(i+1)]))