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 as a string to out.bin, i want to write it as a 4 bytes integer to out.bin.
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.