hex to binary conversion

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

  1. Jeffrey Ross

    Jeffrey Ross Guest

    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
    #1
    1. Advertising

  2. 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
    Tad McClellan, Nov 29, 2004
    #2
    1. Advertising

  3. Jeffrey Ross

    Jeffrey Ross Guest

    "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
    #3
  4. Jeffrey Ross

    Jeffrey Ross Guest

    "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
    #4
  5. 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
    #5
  6. 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
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,181
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    461
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    583
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,837
  5. bob
    Replies:
    6
    Views:
    743
    Peter Shaggy Haywood
    Mar 21, 2006
Loading...

Share This Page