hex to binary conversion

J

Jeffrey Ross

Hi.
I can convert "414243" to "ABC" using awk (see below) but wonder if there's
a simpler way of doing it in Perl.
perl -e 'printf "%c", hex("41")' # works for a single
hard-coded 2-character string

How do I do it for lines of arbitrary length?

My awk equivalent is:

awk 'BEGIN {for (i=0;i<=15;i++){

for (j=0;j<=15;j++){a[sprintf("%x%x",i,j)]=i*16+j}

}

}

{

for (i=1;i<=length($0);i+=2) {

x=tolower(substr($0,i,1))

y=tolower(substr($0,i+1,1))

printf "%c",a[x y]

}

print ""

}'

TIA.

Jeffrey
 
T

Tad McClellan

Jeffrey Ross said:
I can convert "414243" to "ABC" using awk


Did you know that one of the programs that comes with the perl
distribution is an awk-to-perl converter?

man a2p

(but the Perl version is as horrid as the awk version)

a simpler way of doing it in Perl.
perl -e 'printf "%c", hex("41")' # works for a single
hard-coded 2-character string

How do I do it for lines


"lines"?

What "lines"?

of arbitrary length?


You must have meant "strings" instead of "lines", yes?


Assuming that $_ contains the string:

print chr hex for /(..)/g;
or
print map chr hex, /(..)/g;
or
print chr hex for split /(..)/;
or
print map chr hex, split /(..)/;



[snip awk code, we don't want that here where some innocent kid
might see it...]
 
J

Jeffrey Ross

Bob Walton said:
Jeffrey Ross wrote:

...
I can convert "414243" to "ABC" using awk (see below) but wonder if there's
a simpler way of doing it in Perl.
perl -e 'printf "%c", hex("41")' # works for a single
hard-coded 2-character string

How do I do it for lines of arbitrary length?

My awk equivalent is: ...
TIA.

Try:

use strict;
use warnings;
my $string='414243';
$string=~s/([\da-f]{2})/chr(hex($1))/egi;
print $string;

Thanks, Bob.
That's scary (but the sort of thing I wanted)!
Jeffrey.
 
J

Jeffrey Ross

Tad McClellan said:
Did you know that one of the programs that comes with the perl
distribution is an awk-to-perl converter?

man a2p

(but the Perl version is as horrid as the awk version)




"lines"?

What "lines"?




You must have meant "strings" instead of "lines", yes?


Assuming that $_ contains the string:

print chr hex for /(..)/g;
or
print map chr hex, /(..)/g;
or
print chr hex for split /(..)/;
or
print map chr hex, split /(..)/;
Yes, "lines" = "strings".
Thanks for the neat solution.
Jeffrey
 
J

John W. Krahn

Jeffrey said:
I can convert "414243" to "ABC" using awk (see below) but wonder if there's
a simpler way of doing it in Perl.
perl -e 'printf "%c", hex("41")' # works for a single
hard-coded 2-character string

How do I do it for lines of arbitrary length?

$ perl -le' $_ = pack "H*", "414243"; print '
ABC



John
 
M

Mark Jason Dominus

How do I do it for lines of arbitrary length?

Here's a non-scary perl way:

%h2b = (0 => "0000", 1 => "0001", 2 => "0010", 3 => "0011",
4 => "0100", 5 => "0101", 6 => "0110", 7 => "0111",
8 => "1000", 9 => "1001", a => "1010", b => "1011",
c => "1100", d => "1101", e => "1110", f => "1111",
);

$hex = "414243";
($binary = $hex) =~ s/(.)/$h2b{lc $1}/g;
print $binary, "\n";

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top