# hex to binary conversion

Discussion in 'Perl Misc' started by Jeffrey Ross, Nov 29, 2004.

1. ### Jeffrey RossGuest

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

Jeffrey Ross, Nov 29, 2004

Jeffrey Ross <> wrote:

> 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...]

--
Perl programming
Fort Worth, Texas

3. ### Jeffrey RossGuest

"Bob Walton" <> wrote in message
news:41aa9141\$1_3@127.0.0.1...
> 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;
> >
> > Jeffrey

>
> --
> Bob Walton

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

Jeffrey Ross, Nov 29, 2004
4. ### Jeffrey RossGuest

"Tad McClellan" <> wrote in message
news:...
> Jeffrey Ross <> wrote:
>
> > 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 /(..)/;
>

Yes, "lines" = "strings".
Thanks for the neat solution.
Jeffrey

Jeffrey Ross, Nov 29, 2004
5. ### John W. KrahnGuest

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?

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

John
--
use Perl;
program
fulfillment

John W. Krahn, Nov 29, 2004
6. ### Mark Jason DominusGuest

In article <41aa7ee2\$>,
Jeffrey Ross <> wrote:
>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.

Mark Jason Dominus, Nov 29, 2004