I have a hex file with a size of 42 kB. Does the size matter while loading it in a microcontroller? Can I load 42 kB hex file in a ATMEGA32 chip which has a memory of 32 kB?
You can use
avr-size to check the real size of your program:
[jpc@jpc sepack] avr-size sepack.elf text data bss dec hex filename 4396 6 277 4679 1247 sepack.elf [jpc@jpc sepack] avr-size sepack.hex text data bss dec hex filename 0 4402 0 4402 1132 sepack.hex
As you can see it works better with
.elf files since it can also show you both how much ram you need (
bss) and how much flash will be used (
data). With the
.hex file only shows you the second figure (labeling it
The hex file generated is often somewhat human-readable and includes checksum data, both of which inflate the size a few times. I believe that AVRs use the Intel HEX format, while other micro toolchains use other formats; e.g. Codewarrior for Freescale HCS08 provides Motorola S-records, which is extremely similar.
If you open the .hex file with a text editor (Notepad), it should look similar to this, though much longer:
where the various fields are described in the example on Wikipedia. There are only 16 bytes in each of those lines that denote data that will be stored in the micro, the rest is checksum and control data that is just used by the flasher program.
Each byte in a hex file takes two hex digits, plus a few characters per record. Your hex file should fit in the device's flash memory.