Possible Duplicate:
using bash: write bit representation of integer to file
I need to write the size of a file into a binary file. For example:
$ stat -c %s in.txt
68187
$ stat -c %s in.txt >> out.bin
Instead of writing "68187" string to out.bin, i want to write the 4 bytes int representation of 168187 to out.bin.
How can i convert "68187" to 4 bytes int?
This is what I could come up with:
Now depending on endianness you might want to swap the byte order:
Example (decoded, so it's visible):
This is for unsigned int, if the int is signed and the value is negative you have to compute the two's complement. Simple math.
You can use the following function to convert a numeric VALUE into its corresponding character:
You have to convert the byte values individually, after each other in the correct order (endianess) for the machine/architecture that you use. So I guess, a little use of another scripting language that supports binary output would do the job best.
See if this works for you