Say I have 8b1f 0008 0231 49f6 0300 f1f3 75f4 0c72 f775 0850 7676 720c 560d 75f0 02e5 ce00 0861 1302 0000 0000, how can I easily get a binary file from that without copying+pasting into a hex editor?
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
- Null-terminated string, opening file for reading
See xxd.
This version will work with binary format too :
The extra '\r' is just if you're dealing w/ dos text files... and process byte by byte to prevent endians difference if running parts of pipe on different systems.
Use:
Here is the way to reverse "od" output :
All the present answers refer to the convenient
xxd -r
approach, but for situations wherexxd
is not available or convenient here is a more portable (and more flexible, but more verbose and less efficient) solution, using only POSIX shell syntax (it also compensates for odd-number of digits in input):By the way: you don't specify whether your input is big-endian or little-endian, or whether you want big/little-endian output. Usually input such as in your question would be big-endian/network-order (e.g. as created by
od -t x1 -An -v
), and would be expected to transform to big-endian output. I presumexxd
just assumes that default if not told otherwise, and this solution does that too. If byte-swapping is needed, how you do the byte-swapping also depends on the word-size of the system (e.g. 32bit, 64bit) and very rarely the byte-size (you can almost always assume 8bit bytes - octets - though).The below functions use a more complex version of the
binary -> od -> binary
trick to portably byteswap binary data, conditional on system endianness, and accounting for system word-size. The algorithm works for anything up to 72bit word-size (becauseseq -s '' 10
->12345678910
doesn't work):You can use the above to output file contents in bigendian format regardless of system endianness: