Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-09-2019, 02:27 PM
|
#1
|
|
LQ Newbie
Registered: Aug 2018
Posts: 15
Rep: 
|
command xxd - how do i decode binary in ascii???
exemple:
echo "hello world" | xxd -b | cut -c 11-63
output
01101000 01100101 01101100 01101100 01101111 00100000
01110111 01101111 01110010 01101100 01100100 00001010
how do i get xxd to convert binary to text again?
|
|
|
|
12-09-2019, 02:48 PM
|
#2
|
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,986
|
Quote:
Originally Posted by seehrum
exemple:
echo "hello world" | xxd -b | cut -c 11-63
output
01101000 01100101 01101100 01101100 01101111 00100000
01110111 01101111 01110010 01101100 01100100 00001010
how do i get xxd to convert binary to text again?
|
You do know that if you do not do the cut action, xxd shows you the original data already in ASCII?
Code:
$ echo "hello world" | xxd -b -c 10
00000000" 01001000 01100101 01101100 01101100 01101111 00100000 hello
00000006: 01110111 01101111 01110010 01101100 01100100 00001010 world.
|
|
|
|
12-09-2019, 02:55 PM
|
#3
|
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky Linux 9.8 w/Cinnamon 5.8.4 (except on the headless production server)
Posts: 6,001
|
See the -r option in man xxd
|
|
|
|
12-09-2019, 03:13 PM
|
#4
|
|
LQ Guru
Registered: Oct 2004
Distribution: Arch
Posts: 5,818
|
I don't have xxd installed
Code:
a="Hello world"
c=$(od -An -vtx1 <<< "$a")
echo "$c"
48 65 6c 6c 6f 20 77 6f 72 6c 64 0a
perl -ape '$_=pack "(H2)*", @F' <<< "$c"
Hello world
Code:
bin="01101000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100 00001010"
d=$(od -An -vtx1 <<< "$bin")
perl -lape '$_=pack"(B8)*",@F' <<< "$bin"
hello world
|
|
|
|
12-09-2019, 03:16 PM
|
#5
|
|
LQ Newbie
Registered: Aug 2018
Posts: 15
Original Poster
Rep: 
|
i know if i don't cut it it shows in ascii, but I need to revert the binary to ascii
the -r option does not work
|
|
|
|
12-09-2019, 03:34 PM
|
#6
|
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky Linux 9.8 w/Cinnamon 5.8.4 (except on the headless production server)
Posts: 6,001
|
Quote:
Originally Posted by seehrum
i know if i don't cut it it shows in ascii, but I need to revert the binary to ascii
the -r option does not work
|
Ah. Didn't read it all.
Code:
-b | -bits
...
The command line switches -r, -p, -i do not work with this mode.
A very brief web search popped up ascii2bin and bin2ascii further searching may prove fruitful.
|
|
|
1 members found this post helpful.
|
12-09-2019, 03:39 PM
|
#7
|
|
LQ Newbie
Registered: Aug 2018
Posts: 15
Original Poster
Rep: 
|
OK thank you
|
|
|
|
12-09-2019, 04:27 PM
|
#8
|
|
LQ Newbie
Registered: Aug 2018
Posts: 15
Original Poster
Rep: 
|
If anyone has any suggestions for native commands that convert binary to ascii  comment!
|
|
|
|
12-11-2019, 04:48 AM
|
#9
|
|
LQ Veteran
Registered: May 2008
Distribution: CRUX
Posts: 7,369
|
I've always found it somewhat odd that the C Standard and the scanf("%i") format specifier don't support the use of 0b01000001. in the same way that you can use 0x41.
Anyway, Perl one liner found on stackexchange:
Code:
$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"'
01000001 01000010
$ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F'
AB
|
|
|
|
12-11-2019, 05:49 AM
|
#10
|
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,986
|
Quote:
Originally Posted by seehrum
If anyone has any suggestions for native commands that convert binary to ascii  comment!
|
What exactly are you after here?
All files are technically "binary", which is to say that they have ones and zeros.
But for instance if you have 0100 0001 which is also 0x41, this represents binary ASCII capital A.
You can treat that as 65 decimal, 0x41 hex, or the binary pattern.
In the end, if you have binary data bytes which fall within the printable ASCII range, you'll see ASCII characters.
There is no conversion required.
Last edited by rtmistler; 12-11-2019 at 11:46 AM.
|
|
|
|
12-11-2019, 09:23 AM
|
#11
|
|
LQ Guru
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 5,212
|
Maybe 'strtol' of Perl could be used. Something like this would be a start:
Code:
#!/usr/local/bin/perl
use POSIX ('strtol');
my ($num, $chr);
$num = POSIX::strtol ('01000100', 2);
$chr = pack ('c', $num);
printf ("%d 0x%x '%s'\n", $num, $num, $chr);
Edit: more complete code:
Code:
$ xxd -b strtol.pl | cut -b 10-62 | perl ./strtol.pl
#!/usr/local/bin/perl
use POSIX ('strtol');
sub EightBitsToByte {
my $eightbits= $_[0];
my $num = POSIX::strtol ($eightbits, 2);
my $chr = pack ('c', $num);
return $chr;
}
while (<>) {
$_ =~ s/([01]{8})[^01]*/EightBitsToByte($1)/ge;
print $_;
}
Last edited by NevemTeve; 12-12-2019 at 04:58 AM.
|
|
|
|
12-11-2019, 11:45 AM
|
#12
|
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,986
|
Yes if you're writing a program the various strtoXXX() functions can be used. It seems as if the OP here wants a command line to convert in the shell. But my contention is that they do not need to convert something which is already ASCII.
|
|
|
|
12-11-2019, 06:44 PM
|
#13
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,866
|
Quote:
Originally Posted by rtmistler
But my contention is that they do not need to convert something which is already ASCII.
|
This seems a little disingenuous to me. The OP made clear enough by showing expected input and output what kind of conversion they want.
|
|
|
|
12-11-2019, 08:14 PM
|
#14
|
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360
Rep: 
|
This uses bash without any external commands.
Code:
echo '01101000 01100101 01101100 01101100 01101111 00100000
01110111 01101111 01110010 01101100 01100100 00001010' |
while read -n9 byte; do # while read 9 characters
echo -ne \\x$( printf '%x' $((2#$byte)) ) # Convert binary to ascii
done
hello world
$((2#$byte)) converts from base 2 to base 10.
printf converts the base 10 to hex.
echo converts the hex to ascii characters.
This is the same as above except it converts non-printing characters to a dot and formats the output.
The non-printing characters are 00000000 to 00011111, and 01111111.
Code:
count=0
echo 0{0,1}{0,1}{0,1}{0,1}{0,1}{0,1}{0,1} | # Brace expansion creates 128 bytes 00000000 to 01111111
while read -n9 byte; do # while read 9 characters
[[ $byte =~ 000[01]{5}|01111111 ]] && echo -n '.' || # Replace non-printing with a dot or
echo -ne \\x$( printf '%x' $((2#$byte)) ) # convert binary to ascii
((++count%16==0)) && echo # Output 16 characters per line
done
................
................
!"#$%&'()*+,-./
0123456789:;<=>?
@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_
`abcdefghijklmno
pqrstuvwxyz{|}~.
Last edited by Kenhelm; 12-11-2019 at 08:28 PM.
|
|
|
4 members found this post helpful.
|
All times are GMT -5. The time now is 05:06 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|