LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 12-09-2019, 02:27 PM   #1
seehrum
LQ Newbie
 
Registered: Aug 2018
Posts: 15

Rep: Reputation: Disabled
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?
 
Old 12-09-2019, 02:48 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by seehrum View Post
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.
 
Old 12-09-2019, 02:55 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
See the -r option in man xxd
 
Old 12-09-2019, 03:13 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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
 
Old 12-09-2019, 03:16 PM   #5
seehrum
LQ Newbie
 
Registered: Aug 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-09-2019, 03:34 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by seehrum View Post
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.
Old 12-09-2019, 03:39 PM   #7
seehrum
LQ Newbie
 
Registered: Aug 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
OK thank you
 
Old 12-09-2019, 04:27 PM   #8
seehrum
LQ Newbie
 
Registered: Aug 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
If anyone has any suggestions for native commands that convert binary to ascii comment!
 
Old 12-11-2019, 04:48 AM   #9
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
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
 
Old 12-11-2019, 05:49 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by seehrum View Post
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.
 
Old 12-11-2019, 09:23 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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.
 
Old 12-11-2019, 11:45 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 12-11-2019, 06:44 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by rtmistler View Post
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.
 
Old 12-11-2019, 08:14 PM   #14
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux xxd Command Tutorial for Beginners (with Examples) LXer Syndicated Linux News 0 11-19-2018 01:21 PM
procmailrc decode data from base64 encoded emails - trying to decode with uudeview garyrickert Linux - Software 6 10-06-2015 03:41 PM
file command sees regular ASCII text file as ASCII Java program text bbraml Linux - Software 6 08-30-2013 08:52 AM
Can't find xxd for fedora core 5 Butterbroetchen Fedora 6 06-28-2006 07:20 AM
xxd missing? everbloom Linux - Newbie 3 05-14-2005 12:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration