- How to convert a text file to binary file using linux commands
- 3 Answers 3
- How to convert file data to plain hex?
- 3 Answers 3
- convert text file of bits to binary file
- 5 Answers 5
- Input
- Output
- convert a hex string to binary and send with netcat
- 4 Answers 4
- UNIX for Dummies Questions & Answers
- Converting binary file to text file
How to convert a text file to binary file using linux commands
I have hex code of a binary in text (string) format. How do I convert it to a binary file using linux commands like cat and echo ?
I know command following command with create a binary test.bin. But what if this hexcode is in another .txt file ? How do I «cat» the content of text file to «echo» and generate a binary file ?
# echo -e «\x00\x001» > test.bin
3 Answers 3
use xxd -r . it reverts a hexdump to its binary representation.
Edit: The -p parameter is also very useful. It accepts «plain» hexadecimal values, but ignores whitespace and line changes.
So, if you have a plain text dump like this:
You can convert it to binary with:
And then get useful output with something like:
In addition of xxd , you should also look at the packages/commands od and hexdump . All are similar, however each provide slightly different options that will allow you to tailor the output to your desired needs. For example hexdump -C is the traditional hexdump with associated ASCII translation along side.
If you have long text or text in file you can also use the binmake tool that allows you to describe in text format some binary data and generate a binary file (or output to stdout). It allows to change the endianess and number formats and accepts comments.
Its default format is hexadecimal but not limited to this.
First get and compile binmake:
Источник
How to convert file data to plain hex?
How to easily convert to/from plain machine-readable hexadecimal data (without any paddings/offsets/character view) with xdd or hexdump ?
I’m tired of digging of some special format strings (and finding out that it suddenly starts wrapping lines after N characters or skip lines) or writing Perl one-liners every time.
Why is it not as simple as base64 / base64 -d ?
3 Answers 3
If you get tired of writing this every time, create an alias.
How to easily convert to/from plain machine-readable hexadecimal data
This writes a hex encoding of the data in test.txt into new file test.hex. The -p or -plain option makes xxd use «plain» hex format with no spaces between pairs of hex digits (i.e. no spaces between byte values). This converts «abc ABC» to «61626320414243». Without the -p it would convert the text to a 16-bit word oriented traditional hexdump format, which is arguably easier to read but less compact and therefore less suitable as a transmission format and slightly harder to reverse.
This uses the -r or -revert option for reverse operation. The -plain option is used again to indicate that the input hex file is in plain format.
I make the output filename different from the original filename so we can later compare the results with the original file.
The diff command outputs nothing — this means there is no difference between the original and reconstituted file contents.
I’m tired of digging of some special format strings
Use alias or declare functions in your .profile to create mnemonics so you don’t have to remember or dig about in man pages.
or just remember -plain and -revert .
Yes, there are new-line characters in the output. You want to avoid that. You can use the -c or -cols option to specify how long you want the output lines to be to attempt to avoid line-wrapping of the output. -c 0 gives the default length and the man page suggests 256 is the limit but it seems to work beyond that.
The wc wordcount command tells us how many lines, words and characters are in each file.
So 121 lines (880 words, 4603 bytes) of ASCII text were encoded as 1 line of hex digits.
Источник
convert text file of bits to binary file
I have a file instructions.txt with the contents:
How can I create a binary file instructions.bin of the same data as instructions.txt . In other words the .bin file should be the same 192 bits that are in the .txt file, with 32 bits per line. I am using bash on Ubuntu Linux. I was trying to use xxd -b instructions.txt but the output is way longer than 192 bits.
5 Answers 5
oneliner to convert 32-bit strings of ones and zeros into corresponding binary:
- perl -ne will iterate through each line of input file provided on STDIN ( instructions.txt )
- pack(«B32», $_) will take a string list of 32 bits ( $_ which we just read from STDIN), and convert it to binary value (you could alternatively use «b32» if you wanted ascending bit order inside each byte instead of descending bit order; see perldoc -f pack for more details)
- print would then output that converted value to STDOUT, which we then redirect to our binary file instructions.bin
Adding the -r option (reverse mode) to xxd -b does not actually work as intended, because xxd simply does not support combining these two flags (it ignores -b if both are given). Instead, you have to convert the bits to hex yourself first. For example like this:
- The part inside the parentheses creates a bc script. It first sets the input base to binary (2) and the output base to hexadecimal (16). After that, the sed command prints the contents of instructions.txt with a semicolon between each group of 4 bits, which corresponds to 1 hex digit. The result is piped into bc .
- The semicolon is a command separator in bc , so all the script does is print every input integer back out (after base conversion).
- The output of bc is a sequence of hex digits, which can be converted to a file with the usual xxd -r -p .
My original answer was incorrect — xxd cannot accept either -p or -r with -b .
Given that the other answers are workable, and in the interest of «another way«, how about the following:
Input
Output
- cat — unnecessary, but used for clarity
- tr -d $’\n’ — remove all newlines from the input
- read -N 4 nibble — read exactly 4× characters into the nibble variable
- printf ‘%x’ «$((2#$
))» convert the nibble from binary to 1× hex character - $((2#. )) — convert the given value from base 2 (binary) to base 10 (decimal)
- printf ‘%x’ — format the given value from base 10 (decimal) to base 16 (hexadecimal)
- xxd -r -p — reverse ( -r ) a plain dump ( -p ) — from hexadecimal to raw binary
- An unquoted heredoc ( ) is used to get content into the Python code
- This is not efficient if the input becomes large
- cat and tr — used to get a clean (one-line) input
- range(0, len(d), 8) — get a list of numbers from 0 to the end of the string d , stepping 8× characters at a time.
- chr(int(d[i:i+8],2)) — convert the current slice ( d[i:i+8] ) from binary to decimal ( int(. 2) ), and then to a raw character ( chr(. ) )
- [ x for y in z] — list comprehension
- ».join(. ) — convert the list of characters into a single string
- print(. ) — print it
Источник
convert a hex string to binary and send with netcat
I have a binary file that I can send with netcat :
The file contains this:
What I really want to do is send the hex string directly. I’ve tried this:
However, the above command just sends the ascii string directly to nc .
4 Answers 4
I used the -r and -p switches for xxd:
Thanks to inspiration from @Gilles’ answer, here’s a Perl version:
Here a solution without xxd or perl :
If the echo builtin of your shell supports it ( bash and zsh do, but not dash ), you just need to use the right backslash escapes:
If you have /bin/echo from GNU coreutils (nearly standard on Linux systems) or from busybox you can use it, too.
With sed you can generate a escaped pattern:
If you have xxd , that’s easy: it can convert to and from hexadecimal.
I don’t think there’s a reasonable (and reasonably fast) way to convert hexadecimal to binary using only POSIX tools. It can be done fairly easy in Perl. The following script converts hexadecimal to binary, ignoring any input character that isn’t a hexadecimal digit. It complains if an input line contains an odd number of hexadecimal digits.
Источник
UNIX for Dummies Questions & Answers
Converting binary file to text file
Im wondering how I can convert a binary file to a text file?
I have ran the following command to output which type of binary file coding it is
Is there a way I can convert this to a text file?
Many thanks!
Moderator’s Comments: | |
|
What a «binary file» and what a «text file» is is primarily determined by the files contents. So there is no «conversion» between the two, like «convert CSV to Excel format». The latter is just two representations of the same thing — tabular data — but the former are simply different types of contents.
There are (today rather unusual) meanings of «text file» as containing compiled code (calling a relocatible object file before linking «text» was the IBM speak of the seventies) and if you are using the term this way you will have to use a linker. If you are using «text» in the more common meaning of «sequences of characters transporting meaning to a reading human» then there is no (common!) way to transform one into the other.
There are of course programs which use binary representations for text files (simpliest example is a packing program which packs a text file into something binary). In such a case you need to specifically reverse the algorithm (i.e use an unpack-program to restore the text file), but this means to know the used algorithm first. And even this only works if a text file was packed beforehand. Put a binary file in and another binary file will come out, packing or no packing.
If you want to search for text parts inside a binary file, this is possible: use the strings command to list readable strings within otherwise binary files:
Источник