Binary to Hexadecimal conversion

D

Deepu

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

A. Sinan Unur

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
 
G

Gunnar Hjalmarsson

Deepu said:
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
 
D

Doug Miller

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

Jürgen Exner

Deepu said:
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

jl_post

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top