Can we convert a hex file back to .c file?

8.5k Views Asked by At

first .c file is embedded coding written in KEIL IDE from which hex file is generated. Now I want to convert the hex file back to .c file. Is it possible? If YES, HOW?

2

There are 2 best solutions below

0
Tom V On

Sadly, no this is not possible.

You can disassemble the code into an object listing using objdump -D but you would need some knowledge of the target system even to identify which parts are code and data. If you have this and can read assembly code then you can find out what some specific part of the code does, but this is a very time-consuming process.

To convert fully back to a high level language like C you would need a decompiler. Without details of the specific target that you are using then the best thing to advise about this is that you just google search for "decompiler".

The problem is that although the output of the decompiler is valid C, it will probably look nothing like the original input. A hex file does not contain any information about the names of functions or variables, so every object will just have an automatically generated name that doesn't mean very much.

1
Clifford On

Just like you cannot convert a cake into its ingredients, you cannot convert machine code to the source code that created it.

Much of what appears in source code is for the benefit of the human or to facilitate the compile/link process and is discarded in the hex file.

Attempts at "decompilers" do exist, but are academic or experimental in nature, or work with code generated by specific compilers for specific and would struggle with code that has been generated with compiler optimisation. Moreover such tools work with object files (.axf in Keil ARM) which may retain some metadata on which the decompiler can work, such as entry point or possibly symbol or debug information. A hex file is simply a binary image encoding and has all that metadata stripped - it is just machine code instructions and program data.