Hi All,

My aim is to supply a hexadecimal value and print its binary value.

I have written a small piece of code for the same:

$input = 23;     #any decimal value
$hex1 = sprintf ("%x",$input);
$binary = unpack 'B*', pack 'H*', $hex1;
@fields1 = unpack 'A4A4', $binary;
print "$fields1[1]   $fields1[0]";    # i need to print the Lower Nibble
first and then the Higher nibble

Output:
0111  0010

This works fine for any double digit number.

But this logic does not work with single digit number as input. When i give
a single digit number (for eg: 5) as input, it is stored as 01010000 in
$binary variable instead of 00000101.

Can anyone guide me in correcting this problem?

Thanks and Regards,
Dharshana

Reply via email to