LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   command xxd - how do i decode binary in ascii??? (https://www.linuxquestions.org/questions/programming-9/command-xxd-how-do-i-decode-binary-in-ascii-4175665681/)

seehrum 12-09-2019 02:27 PM

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?

rtmistler 12-09-2019 02:48 PM

Quote:

Originally Posted by seehrum (Post 6066234)
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.


scasey 12-09-2019 02:55 PM

See the -r option in man xxd

teckk 12-09-2019 03:13 PM

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


seehrum 12-09-2019 03:16 PM

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

scasey 12-09-2019 03:34 PM

Quote:

Originally Posted by seehrum (Post 6066255)
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.

seehrum 12-09-2019 03:39 PM

OK thank you

seehrum 12-09-2019 04:27 PM

If anyone has any suggestions for native commands that convert binary to ascii ;) comment!

GazL 12-11-2019 04:48 AM

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


rtmistler 12-11-2019 05:49 AM

Quote:

Originally Posted by seehrum (Post 6066276)
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.

NevemTeve 12-11-2019 09:23 AM

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 $_;
}


rtmistler 12-11-2019 11:45 AM

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.

ntubski 12-11-2019 06:44 PM

Quote:

Originally Posted by rtmistler (Post 6066871)
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.

Kenhelm 12-11-2019 08:14 PM

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{|}~.



All times are GMT -5. The time now is 05:02 PM.