You need to understand that the ACII/Hex representation is only that: a representation. If you save that as is, the resulting file will be more than twice as large as the original binary file.
If you have a hex representation of a binary file, the correct way to store it works like that:
1. remove the decoration, e. g. whitespace, linebreaks, line numbers, offset addresses, and similar bits that your hex representation may include. E. g. part of a representation may look like this:
000000D8 48 45 4C 4C 4F 20 57 4F HELLO WO
000000E0 52 4C 44 21 00 00 00 00 RLD!
Here the first long number in each line is the offset to the file start (in hex)), the following eight pairs of alphanumericals represent the value of one byte each, and the last 8 characters show the ASCII interpretation of that byte sequence.
To store that, you need to compress it to just the byte values, i. e. :
48454C4C4F20574F524C442100000000
2. Convert the characters of this byte representation into hexadecimal values. e. g. the character '4' corresponds to the hex value 4, and 'C' corresponds to 12.
3. Convert the hex representation of the byte sequence into actual bytes: since each character of a byte representation represents a nibble of the actual byte, the corresponding byte value for the hexadeciaml digit sequence h1, h2 can be calculated as
unsigned char byte_value = 16*h1 + h2;
So, for example, the byte value of the digit sequence {4, 12} is 4*16+12 = 76
4. Store the resulting bytes in consecutive order