Binary to Hexadecimal conversion

Discussion in 'Perl Misc' started by Deepu, Dec 15, 2007.

  1. Deepu

    Deepu Guest

    Hi All,

    I have a $test with 32 bit binary number.

    $test = 00000000011111000000010111100100

    How can i get the hexadecimal number for this.

    Thanks for the help..
    Deepu, Dec 15, 2007
    #1
    1. Advertising

  2. Deepu <> wrote in news:a9f54740-4cb7-4f06-a8e6-
    :

    > Hi All,
    >
    > I have a $test with 32 bit binary number.
    >
    > $test = 00000000011111000000010111100100


    No, you don't.


    C:\DOCUME~1\asu1\LOCALS~1\Temp> cat t.pl
    #!/usr/bin/perl

    use strict;
    use warnings;

    my $test = 00000000011111000000010111100100;

    C:\DOCUME~1\asu1\LOCALS~1\Temp> t
    Integer overflow in octal number at C:\DOCUME~1\asu1\LOCALS~1\Temp\t.pl
    line 6.
    Octal number > 037777777777 non-portable at C:\DOCUME~1\asu1\LOCALS~1
    \Temp\t.pl
    line 6.

    You have something else.

    We don't know what you have. It seems like you don't know it either.

    > How can i get the hexadecimal number for this.


    perldoc -f sprintf

    Check out %x and %X format specifiers.

    Now, maybe you have a string which contains the binary representation of
    an integer:

    my $test = '00000000011111000000010111100100';

    In that case, use oct (see perldoc -f oct):

    C:\DOCUME~1\asu1\LOCALS~1\Temp> cat t.pl
    #!/usr/bin/perl

    use strict;
    use warnings;

    my $test = '00000000011111000000010111100100';

    printf "%8.8x\n", oct( "0b$test" );


    C:\DOCUME~1\asu1\LOCALS~1\Temp> t
    007c05e4

    Sinan



    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)
    clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>
    A. Sinan Unur, Dec 15, 2007
    #2
    1. Advertising

  3. Deepu wrote:
    > I have a $test with 32 bit binary number.
    >
    > $test = 00000000011111000000010111100100
    >
    > How can i get the hexadecimal number for this.


    That's a FAQ.

    perldoc -q numeric

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 15, 2007
    #3
  4. Deepu

    Doug Miller Guest

    In article <>, Deepu <> wrote:
    >Hi All,
    >
    >I have a $test with 32 bit binary number.


    Really?
    >
    >$test = 00000000011111000000010111100100


    After discarding the leading zeros, it looks more like a 23-digit decimal
    number to me... it just doesn't happen to contain any decimal digits greater
    than one.

    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
    Doug Miller, Dec 15, 2007
    #4
  5. Deepu wrote:
    > I have a $test with 32 bit binary number.
    >
    > $test = 00000000011111000000010111100100


    Actually that is not a binary number but the octal representation of approx.
    8.43253994239781e+019

    > How can i get the hexadecimal number for this.


    There is no such thing as a hexadecimal number. Numbers are abstract
    concepts. 13, 0xD, thirteen, 0b1101, 015, etc, etc, are just different
    representations(!) of the same number.

    If you are looking for the hexadecimal representation of a specific number
    please see printf().

    jue
    Jürgen Exner, Dec 15, 2007
    #5
  6. Deepu

    Guest

    On Dec 14, 7:54 pm, Deepu <> wrote:
    >
    > I have a $test with 32 bit binary number.
    >
    > $test = 00000000011111000000010111100100
    >
    > How can i get the hexadecimal number for this.



    Dear Deepu,

    First of all, when you specify an integer literal that starts with
    a zero, Perl interprets that as an octal number. For example, setting
    $x to 0755 will set it to the decimal value of 493 (which is not the
    same number as 755).

    So you probably want to use the following line instead where the
    binary number is quoted (so Perl won't assume it's in octal):

    $test = "00000000011111000000010111100100";

    or better yet, use the "0b" prefix to let Perl know you're specifying
    a binary number:

    $test = 0b00000000_01111100_00000101_11100100;

    (This method allows you to put in optional underscores to make it
    easier to read the bits.)

    If you use the first approach, you can extract a hexadecimal string
    like this:

    my $test = "00000000011111000000010111100100";
    my $value = oct("0b$test"); # converts to numerical value
    my $hexString = sprintf('%x', $value);

    The second approach is even easier, as the "0b" prefix lets Perl
    know exactly what the number is supposed to be (so we don't have to
    use a step to convert it to a numerical value):

    my $test = 0b00000000_01111100_00000101_11100100;
    my $hexString = sprintf('%x', $test);

    Both these approaches set $hexString to "7c05e4". If for some
    reason you want the leading zeros (so that you have "007c05e4"), then
    change '%x' to '%08x'.

    I hope this helps, Deepu.

    -- Jean-Luc
    , Dec 17, 2007
    #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. Victor Hannak
    Replies:
    0
    Views:
    7,438
    Victor Hannak
    Feb 20, 2004
  2. Bengt Richter

    Hexadecimal Conversion in Python

    Bengt Richter, Nov 2, 2005, in forum: Python
    Replies:
    10
    Views:
    51,196
    Peter Hansen
    Nov 2, 2005
  3. lei
    Replies:
    10
    Views:
    770
    Don Roby
    Dec 30, 2006
  4. hexadecimal to float conversion

    , Aug 19, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,718
    Keith Thompson
    Aug 20, 2005
  5. SM Ryan

    Re: hexadecimal to float conversion

    SM Ryan, Aug 20, 2005, in forum: C Programming
    Replies:
    3
    Views:
    570
    Keith Thompson
    Aug 25, 2005
Loading...

Share This Page