hex to binary

Discussion in 'Perl Misc' started by Venkatesh can....can..., Mar 18, 2008.

  1. How to convert hexadecimal number to binary in perl ??
     
    Venkatesh can....can..., Mar 18, 2008
    #1
    1. Advertising

  2. Venkatesh can....can... wrote:
    > How to convert hexadecimal number to binary in perl ??


    perldoc -f hex


    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
     
    Josef Moellers, Mar 18, 2008
    #2
    1. Advertising

  3. "Venkatesh can....can..." <> wrote:
    >How to convert hexadecimal number to binary in perl ??


    You must have missed the current discussion titled "decrease MAC".
    There are no hexadicimal numbers but only numbers in hexadecimal
    representation. And the same for binary.
    So, use hex() to convert the string into a number and then sprintf() to
    convert the number into a binary representation as string.

    jue
     
    Jürgen Exner, Mar 18, 2008
    #3
  4. Venkatesh can....can...

    Guest

    On Mar 18, 8:37 pm, "Venkatesh can....can..."
    <> wrote:
    > How to convert hexadecimal number to binary in perl ??


    #! perl
    use warnings;
    use strict;

    # Create the stuff for matching

    my $matches = "0123456789ABCDEF";
    my %hex2bin;
    for (my $i=0;$i<16;$i++) {
    $hex2bin{substr($matches,$i,1)}=int($i/8).int(($i/4)%2).int(($i/
    2)%2).int($i%2);
    }

    # Now test

    my $hex_string = "1234BAad12345";
    print "Before: $hex_string\n";
    $hex_string =~ s/([$matches])/$hex2bin{uc($1)}/gi;
    print "After: $hex_string\n";
     
    , Mar 19, 2008
    #4
    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,223
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    590
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,885
  5. tim
    Replies:
    2
    Views:
    1,566
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page