Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - convert hexadecimal to binary and use it as string

I am new to Perl and I have difficulties using the different types.

I am trying to get an hexadecimal register, transform it to binary, use it a string and get substrings from the binary string.

I have done a few searches and what I tried is :

my $hex = 0xFA1F;
print "$hex\n";

result was "64031" . First surprise : can't I print the hex value in Perl and not just the decimal value ?

$hex = hex($hex);
print "$hex\n";

Result was 409649. Second surprise : I would expect the result to be also 64031 since "hex" converts hexadecimal to decimal.

my $bin = printf("%b", $hex);

It prints the binary value. Is there a way to transform the hex to bin without printing it ?

Thanks, SLP

like image 755
SLP Avatar asked Mar 07 '19 08:03

SLP


People also ask

Can hex convert to binary directly?

Hexadecimal to binarySplit the hex number into individual values. Convert each hex value into its decimal equivalent. Next, convert each decimal digit into binary, making sure to write four digits for each value. Combine all four digits to make one binary number.

How do you represent a hex in Perl?

Interprets EXPR as a hex string and returns the corresponding numeric value. If EXPR is omitted, uses $_ . A hex string consists of hex digits and an optional 0x or x prefix. Each hex digit may be preceded by a single underscore, which will be ignored.

Why is it easy to convert from hexadecimal to binary?

Converting between hex and binary is easy, because each digit of a hexadecimal number "maps" to four bits (a bit being an individual binary digit) of a binary value. So a byte -- eight binary digits -- can always be represented by two hexadecimal digits.


3 Answers

Decimal, binary, and hexadecimal are all text representations of a number (i.e. ways of writing a number). Computers can't deal with these as numbers.

my $num = 0xFA1F; stores the specified number (sixty-four thousand and thirty-one) into $num. It's stored in a format the hardware understands, but that's not very important. What's important is that it's stored as a number, not text.

When print is asked to print a number, it prints it out in decimal (or scientific notation if large/small enough). It has no idea how the number of created (from a hex constant? from addition? etc), so it can't determine how to output the number based on that.

To print an number as hex, you can use

my $hex = 'FA1F';   # $hex contains the hex representation of the number.
print $hex;         # Prints the hex representation of the number.

or

my $num = 0xFA1F;   # $num contains the number.
printf "%X", $num;  # Prints the hex representation of the number.
like image 119
ikegami Avatar answered Oct 06 '22 03:10

ikegami


You are assigning a integer value using hexadecimal format. print by default prints numbers in decimal format, so you are getting 64031.

You can verify this using the printf() by giving different formats.

$ perl -e ' my $num = 0xFA1F; printf("%d %X %b\n", ($num) x 3 ) '
64031 FA1F 1111101000011111

$ perl -e ' my $num = 64031; printf("%d %X %b\n", ($num) x 3 ) '
64031 FA1F 1111101000011111

$ perl -e ' my $num = 0b1111101000011111; printf("%d %X %b\n", ($num) x 3 ) '
64031 FA1F 1111101000011111

$

To get the binary format of 0xFA1F in string, you can use sprintf()

$ perl -e ' my $hex = 0xFA1F; my $bin=sprintf("%b",$hex) ; print "$bin\n" '
1111101000011111

$
like image 45
stack0114106 Avatar answered Oct 06 '22 02:10

stack0114106


lets take each bit of confusion in order

my $hex = 0xFA1F;

This stores a hex constant in $hex, but Perl doesn't have a hex data type so although you can write hex constants, and binary and octal constants for that matter, Perl converts them all to decimal. Note that there is a big difference between

my $hex = 0xFA1F;

and

my $hex = '0xFA1F';

The first stores a number into $hex, which when you print it out you get a decimal number, the second stores a string which when printed out will give 0xFAF1 but can be passed to the hex() function to be converted to decimal.

$hex = hex($hex);

The hex function converts a string as if it was a hex number and returns the decimal value and, as up to this point, $hex has only ever been used as a number Perl will first stringify $hex then pass the string to the hex() function to convert that value from hex to decimal.

So to the solution. You are almost there with printf(),there is a function called sprintf() which takes the same parameters as printf() but instead of printing the formatted value returns it as a string. So what you need is.

my $hex = 0xFA1F;
my $bin = sprintf("%b", $hex);
print $bin;

Technical note: Yes I know that Perl stores all its numbers internally as binary, but lets not go there for this answer, OK?

like image 37
JGNI Avatar answered Oct 06 '22 04:10

JGNI