convert text file of bits to binary file
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I have a file instructions.txt
with the contents:
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
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.
linux bash binary-files xxd
New contributor
add a comment |Â
up vote
8
down vote
favorite
I have a file instructions.txt
with the contents:
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
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.
linux bash binary-files xxd
New contributor
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I have a file instructions.txt
with the contents:
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
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.
linux bash binary-files xxd
New contributor
I have a file instructions.txt
with the contents:
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
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.
linux bash binary-files xxd
linux bash binary-files xxd
New contributor
New contributor
edited 10 hours ago
Attie
10.2k32338
10.2k32338
New contributor
asked 10 hours ago
DavOS
1435
1435
New contributor
New contributor
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
6
down vote
accepted
You were on the right track with xxd -b
, but you need some other options too:
-p
- use the "plain hexdump style"-r
- use the "reverse operation"
Be careful, because xxd
doesn't accept multiple options (e.g: -bpr
) - they need to be given separately.
$ cat instructions.txt
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
$ xxd -b -p -r < instructions.txt > instructions.bin
$ hexdump -Cv < instructions.bin
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000010 00 00 00 10 11 01 00 01 00 10 00 00 10 00 00 11 |................|
00000020 00 00 00 00 01 11 00 11 00 00 00 10 10 11 00 11 |................|
00000030 00 00 00 00 01 11 00 11 00 00 01 00 00 11 00 11 |................|
00000040 00 00 00 00 01 11 00 11 01 10 01 00 10 11 00 11 |................|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000060
Thank you so so much you are awesome! This is exactly what I was looking for!
â DavOS
9 hours ago
1
Careful - this reads 0 and 1 as hex digits, not as bits. From xxd's manpage about the -b flag: "The command line switches -r, -p, -i do not work with this mode."
â nomadictype
3 hours ago
add a comment |Â
up vote
0
down vote
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:
( echo 'obase=16;ibase=2'; sed -Ee 's/[01]4/;/g' instructions.txt ) | bc | xxd -r -p > instructions.bin
Full explanation:
- 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, thesed
command prints the contents ofinstructions.txt
with a semicolon between each group of 4 bits, which corresponds to 1 hex digit. The result is piped intobc
. - 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 usualxxd -r -p
.
Output:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
$ xxd -b -c4 instructions.bin
00000000: 00000000 00000000 00000000 00010011 ....
00000004: 00000010 11010001 00100000 10000011 .. .
00000008: 00000000 01110011 00000010 10110011 .s..
0000000c: 00000000 01110011 00000100 00110011 .s.3
00000010: 00000000 01110011 01100100 10110011 .sd.
00000014: 00000000 00000000 00000000 00010011 ....
New contributor
Sorry, there's still an endianness bug in this. Working on fixing it!
â nomadictype
2 hours ago
Actually, it's fine. I was confused earlier by using the wrong output width in the last xxd command.
â nomadictype
2 hours ago
add a comment |Â
up vote
0
down vote
oneliner to convert 32-bit strings of ones and zeros into corresponding binary:
$ perl -ne 'print pack("B32", $_)' < instructions.txt > instructions.bin
verify:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You were on the right track with xxd -b
, but you need some other options too:
-p
- use the "plain hexdump style"-r
- use the "reverse operation"
Be careful, because xxd
doesn't accept multiple options (e.g: -bpr
) - they need to be given separately.
$ cat instructions.txt
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
$ xxd -b -p -r < instructions.txt > instructions.bin
$ hexdump -Cv < instructions.bin
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000010 00 00 00 10 11 01 00 01 00 10 00 00 10 00 00 11 |................|
00000020 00 00 00 00 01 11 00 11 00 00 00 10 10 11 00 11 |................|
00000030 00 00 00 00 01 11 00 11 00 00 01 00 00 11 00 11 |................|
00000040 00 00 00 00 01 11 00 11 01 10 01 00 10 11 00 11 |................|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000060
Thank you so so much you are awesome! This is exactly what I was looking for!
â DavOS
9 hours ago
1
Careful - this reads 0 and 1 as hex digits, not as bits. From xxd's manpage about the -b flag: "The command line switches -r, -p, -i do not work with this mode."
â nomadictype
3 hours ago
add a comment |Â
up vote
6
down vote
accepted
You were on the right track with xxd -b
, but you need some other options too:
-p
- use the "plain hexdump style"-r
- use the "reverse operation"
Be careful, because xxd
doesn't accept multiple options (e.g: -bpr
) - they need to be given separately.
$ cat instructions.txt
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
$ xxd -b -p -r < instructions.txt > instructions.bin
$ hexdump -Cv < instructions.bin
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000010 00 00 00 10 11 01 00 01 00 10 00 00 10 00 00 11 |................|
00000020 00 00 00 00 01 11 00 11 00 00 00 10 10 11 00 11 |................|
00000030 00 00 00 00 01 11 00 11 00 00 01 00 00 11 00 11 |................|
00000040 00 00 00 00 01 11 00 11 01 10 01 00 10 11 00 11 |................|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000060
Thank you so so much you are awesome! This is exactly what I was looking for!
â DavOS
9 hours ago
1
Careful - this reads 0 and 1 as hex digits, not as bits. From xxd's manpage about the -b flag: "The command line switches -r, -p, -i do not work with this mode."
â nomadictype
3 hours ago
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You were on the right track with xxd -b
, but you need some other options too:
-p
- use the "plain hexdump style"-r
- use the "reverse operation"
Be careful, because xxd
doesn't accept multiple options (e.g: -bpr
) - they need to be given separately.
$ cat instructions.txt
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
$ xxd -b -p -r < instructions.txt > instructions.bin
$ hexdump -Cv < instructions.bin
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000010 00 00 00 10 11 01 00 01 00 10 00 00 10 00 00 11 |................|
00000020 00 00 00 00 01 11 00 11 00 00 00 10 10 11 00 11 |................|
00000030 00 00 00 00 01 11 00 11 00 00 01 00 00 11 00 11 |................|
00000040 00 00 00 00 01 11 00 11 01 10 01 00 10 11 00 11 |................|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000060
You were on the right track with xxd -b
, but you need some other options too:
-p
- use the "plain hexdump style"-r
- use the "reverse operation"
Be careful, because xxd
doesn't accept multiple options (e.g: -bpr
) - they need to be given separately.
$ cat instructions.txt
00000000000000000000000000010011
00000010110100010010000010000011
00000000011100110000001010110011
00000000011100110000010000110011
00000000011100110110010010110011
00000000000000000000000000010011
$ xxd -b -p -r < instructions.txt > instructions.bin
$ hexdump -Cv < instructions.bin
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000010 00 00 00 10 11 01 00 01 00 10 00 00 10 00 00 11 |................|
00000020 00 00 00 00 01 11 00 11 00 00 00 10 10 11 00 11 |................|
00000030 00 00 00 00 01 11 00 11 00 00 01 00 00 11 00 11 |................|
00000040 00 00 00 00 01 11 00 11 01 10 01 00 10 11 00 11 |................|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 11 |................|
00000060
answered 9 hours ago
Attie
10.2k32338
10.2k32338
Thank you so so much you are awesome! This is exactly what I was looking for!
â DavOS
9 hours ago
1
Careful - this reads 0 and 1 as hex digits, not as bits. From xxd's manpage about the -b flag: "The command line switches -r, -p, -i do not work with this mode."
â nomadictype
3 hours ago
add a comment |Â
Thank you so so much you are awesome! This is exactly what I was looking for!
â DavOS
9 hours ago
1
Careful - this reads 0 and 1 as hex digits, not as bits. From xxd's manpage about the -b flag: "The command line switches -r, -p, -i do not work with this mode."
â nomadictype
3 hours ago
Thank you so so much you are awesome! This is exactly what I was looking for!
â DavOS
9 hours ago
Thank you so so much you are awesome! This is exactly what I was looking for!
â DavOS
9 hours ago
1
1
Careful - this reads 0 and 1 as hex digits, not as bits. From xxd's manpage about the -b flag: "The command line switches -r, -p, -i do not work with this mode."
â nomadictype
3 hours ago
Careful - this reads 0 and 1 as hex digits, not as bits. From xxd's manpage about the -b flag: "The command line switches -r, -p, -i do not work with this mode."
â nomadictype
3 hours ago
add a comment |Â
up vote
0
down vote
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:
( echo 'obase=16;ibase=2'; sed -Ee 's/[01]4/;/g' instructions.txt ) | bc | xxd -r -p > instructions.bin
Full explanation:
- 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, thesed
command prints the contents ofinstructions.txt
with a semicolon between each group of 4 bits, which corresponds to 1 hex digit. The result is piped intobc
. - 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 usualxxd -r -p
.
Output:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
$ xxd -b -c4 instructions.bin
00000000: 00000000 00000000 00000000 00010011 ....
00000004: 00000010 11010001 00100000 10000011 .. .
00000008: 00000000 01110011 00000010 10110011 .s..
0000000c: 00000000 01110011 00000100 00110011 .s.3
00000010: 00000000 01110011 01100100 10110011 .sd.
00000014: 00000000 00000000 00000000 00010011 ....
New contributor
Sorry, there's still an endianness bug in this. Working on fixing it!
â nomadictype
2 hours ago
Actually, it's fine. I was confused earlier by using the wrong output width in the last xxd command.
â nomadictype
2 hours ago
add a comment |Â
up vote
0
down vote
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:
( echo 'obase=16;ibase=2'; sed -Ee 's/[01]4/;/g' instructions.txt ) | bc | xxd -r -p > instructions.bin
Full explanation:
- 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, thesed
command prints the contents ofinstructions.txt
with a semicolon between each group of 4 bits, which corresponds to 1 hex digit. The result is piped intobc
. - 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 usualxxd -r -p
.
Output:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
$ xxd -b -c4 instructions.bin
00000000: 00000000 00000000 00000000 00010011 ....
00000004: 00000010 11010001 00100000 10000011 .. .
00000008: 00000000 01110011 00000010 10110011 .s..
0000000c: 00000000 01110011 00000100 00110011 .s.3
00000010: 00000000 01110011 01100100 10110011 .sd.
00000014: 00000000 00000000 00000000 00010011 ....
New contributor
Sorry, there's still an endianness bug in this. Working on fixing it!
â nomadictype
2 hours ago
Actually, it's fine. I was confused earlier by using the wrong output width in the last xxd command.
â nomadictype
2 hours ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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:
( echo 'obase=16;ibase=2'; sed -Ee 's/[01]4/;/g' instructions.txt ) | bc | xxd -r -p > instructions.bin
Full explanation:
- 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, thesed
command prints the contents ofinstructions.txt
with a semicolon between each group of 4 bits, which corresponds to 1 hex digit. The result is piped intobc
. - 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 usualxxd -r -p
.
Output:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
$ xxd -b -c4 instructions.bin
00000000: 00000000 00000000 00000000 00010011 ....
00000004: 00000010 11010001 00100000 10000011 .. .
00000008: 00000000 01110011 00000010 10110011 .s..
0000000c: 00000000 01110011 00000100 00110011 .s.3
00000010: 00000000 01110011 01100100 10110011 .sd.
00000014: 00000000 00000000 00000000 00010011 ....
New contributor
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:
( echo 'obase=16;ibase=2'; sed -Ee 's/[01]4/;/g' instructions.txt ) | bc | xxd -r -p > instructions.bin
Full explanation:
- 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, thesed
command prints the contents ofinstructions.txt
with a semicolon between each group of 4 bits, which corresponds to 1 hex digit. The result is piped intobc
. - 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 usualxxd -r -p
.
Output:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
$ xxd -b -c4 instructions.bin
00000000: 00000000 00000000 00000000 00010011 ....
00000004: 00000010 11010001 00100000 10000011 .. .
00000008: 00000000 01110011 00000010 10110011 .s..
0000000c: 00000000 01110011 00000100 00110011 .s.3
00000010: 00000000 01110011 01100100 10110011 .sd.
00000014: 00000000 00000000 00000000 00010011 ....
New contributor
edited 2 hours ago
New contributor
answered 2 hours ago
nomadictype
1012
1012
New contributor
New contributor
Sorry, there's still an endianness bug in this. Working on fixing it!
â nomadictype
2 hours ago
Actually, it's fine. I was confused earlier by using the wrong output width in the last xxd command.
â nomadictype
2 hours ago
add a comment |Â
Sorry, there's still an endianness bug in this. Working on fixing it!
â nomadictype
2 hours ago
Actually, it's fine. I was confused earlier by using the wrong output width in the last xxd command.
â nomadictype
2 hours ago
Sorry, there's still an endianness bug in this. Working on fixing it!
â nomadictype
2 hours ago
Sorry, there's still an endianness bug in this. Working on fixing it!
â nomadictype
2 hours ago
Actually, it's fine. I was confused earlier by using the wrong output width in the last xxd command.
â nomadictype
2 hours ago
Actually, it's fine. I was confused earlier by using the wrong output width in the last xxd command.
â nomadictype
2 hours ago
add a comment |Â
up vote
0
down vote
oneliner to convert 32-bit strings of ones and zeros into corresponding binary:
$ perl -ne 'print pack("B32", $_)' < instructions.txt > instructions.bin
verify:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
add a comment |Â
up vote
0
down vote
oneliner to convert 32-bit strings of ones and zeros into corresponding binary:
$ perl -ne 'print pack("B32", $_)' < instructions.txt > instructions.bin
verify:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
add a comment |Â
up vote
0
down vote
up vote
0
down vote
oneliner to convert 32-bit strings of ones and zeros into corresponding binary:
$ perl -ne 'print pack("B32", $_)' < instructions.txt > instructions.bin
verify:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
oneliner to convert 32-bit strings of ones and zeros into corresponding binary:
$ perl -ne 'print pack("B32", $_)' < instructions.txt > instructions.bin
verify:
$ hexdump -Cv instructions.bin
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
answered 1 hour ago
Matija Nalis
1,514716
1,514716
add a comment |Â
add a comment |Â
DavOS is a new contributor. Be nice, and check out our Code of Conduct.
DavOS is a new contributor. Be nice, and check out our Code of Conduct.
DavOS is a new contributor. Be nice, and check out our Code of Conduct.
DavOS is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1365264%2fconvert-text-file-of-bits-to-binary-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password