- How to convert a text file to binary file using linux commands
- 3 Answers 3
- convert a hex string to binary and send with netcat
- 4 Answers 4
- convert text file of bits to binary file
- 5 Answers 5
- Input
- Output
- Linux printf to convert Decimal to Binary?
- 2 Answers 2
- ASCII to Binary and Binary to ASCII conversion tools?
- 6 Answers 6
- Converting bases
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:
Источник
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.
Источник
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
Источник
Linux printf to convert Decimal to Binary?
It’s very easy to convert Decimal number to Hex or Oct using printf function in Linux with %x and %o format specifier respectively.
I was trying to convert similar decimal number to binary but it didn’t work
Is it possible to convert Dec to Bin using printf function in Linux?
What else (number conversion) can be done with Linux printf ?
Hex to Decimal? Hex to Octal? Hex to Binary?
2 Answers 2
Cannot find a way with printf. You can use dc instead.
dec to bin = 11111111
Is it possible to convert Dec to Bin using printf function in Linux?
No, C printf does not have format modifier for binary formatting ( http://www.cplusplus.com/reference/cstdio/printf/ ), and the linux util printf doesn’t extend it with it either (the %b is «expand backslash escape sequences in the corresponding argument»). The printf tool may be still overshadowed by your particular shell, which may eventually add more formatters, then you have to check manual of your shell (for example bash does extend it somewhat, but still no «binary» formatter).
What else (number conversion) can be done with Linux printf?
Hex to Decimal? Hex to Octal? Hex to Binary?
It should support common C-formatted inputs (i.e. 0xN for hexadecimal input, and 0N for octal input), for example:
So yes, you can basically convert decimal/hex/octal to decimal/hex/octal, any combination, by using desired input format and output format.
You are actually sort of thinking about it wrong, it’s separate input to binary conversion, and binary to output, i.e. when you put «16» as argument value for printf, it’s passed as string to the tool, it will then parse the string by value parser, detecting «16» is decimal format, converting it to native integer value 16 (i.e. binary inside CPU), and then it will finally convert it back to string according to the format specifier in the format string, i.e. «%#x» will produce hexadecimal formatting of that internal (binary) integer value («0x10»).
If you want «binary» output for yourself to check the value, seasoned assembly programmers don’t bother with that, they learn to read hexadecimal per separate bits, because in hexadecimal every digit is formed by exactly 4 bits (0000 = 0x0, 0001 = 0x1, . , 1110 = 0xE, 1111 = 0xF), so by using hexadecimal formatting you can quite easily read any particular bit you are interested into. While the true binary output makes it a bit easier by showing the 0/1 directly, but with 32 and 64 bit values it’s a bit too long and makes it harder to stay position aware, this part is somewhat simpler in hexadecimal (as each two digits are 8 bits = one byte).
Источник
ASCII to Binary and Binary to ASCII conversion tools?
Which is a good tool to convert ASCII to binary, and binary to ASCII?
I was hoping for something like:
Or, more realistic:
And also the reverse:
PS: I’m using bash
PS2: I hope I didn’t get the wrong binary
6 Answers 6
- -e expression evaluate the given expression as perl code
- -p : sed mode. The expression is evaluated for each line of input, with the content of the line stored in the $_ variable and printed after the evaluation of the expression.
- -l : even more like sed : instead of the full line, only the content of the line (that is, without the line delimiter) is in $_ (and a newline is added back on output). So perl -lpe code works like sed code except that it’s perl code as opposed to sed code.
- unpack «B*» works on the $_ variable by default and extracts its content as a bit string walking from the highest bit of the first byte to the lowest bit of the last byte.
- pack does the reverse of unpack . See perldoc -f pack for details.
(it assumes the input is in blocks of 8 bits (0-padded)).
With unpack «(B8)*» , we extract 8 bits at a time, and we join the resulting strings with spaces with join » » .
You can use xxd to convert from ASCII and binary.
Converting bases
If you’re looking to do just base conversions between Hex, Octal, & Dec I usually use the basic calculator command line tool ( bc ) to do such things. Note that bc is always very picky about the correct order of bases: you have to specify the resulting base ( obase ) first, then add your choice of ibase .
Источник