Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > hex to binary conversion

Reply
Thread Tools

hex to binary conversion

 
 
Jeffrey Ross
Guest
Posts: n/a
 
      11-29-2004
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



 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-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...]


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Jeffrey Ross
Guest
Posts: n/a
 
      11-29-2004
"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.


 
Reply With Quote
 
Jeffrey Ross
Guest
Posts: n/a
 
      11-29-2004
"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


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-29-2004
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
 
Reply With Quote
 
Mark Jason Dominus
Guest
Posts: n/a
 
      11-29-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie: hex, decimal, binary conversion bob C Programming 6 03-21-2006 01:51 AM
OT: Anyone have a good site for learning hex to binary conversion? Nocturnal MCSE 45 12-05-2005 07:57 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57