Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Multiply Hex values

by kepler (Scribe)
on Feb 09, 2017 at 11:17 UTC ( #1181504=perlquestion: print w/replies, xml ) Need Help??

kepler has asked for the wisdom of the Perl Monks concerning the following question:

Good morning

I would need a little help on this subject - I apreciate in advance. If I have two strings that represent to hexadecimal values, let's say "12340F" and "33", how can I multiply them and get the result in hexadecimal too? I'm just asking this because of course there's a way of doing it by reducing the 2 values to decimal, multiply and convert to hex again. I was just wondering if there's a way of doing it directly in Perl

Kind regards

Kepler

Replies are listed 'Best First'.
Re: Multiply Hex values
by Corion (Pope) on Feb 09, 2017 at 11:31 UTC

    Don't reduce them to "decimal", convert them to numbers (hex), multiply them and convert back to hexadecimal representation (sprintf):

    perl -wle "print sprintf '%08x', hex(shift)*hex(shift)" 12340f 33
      Hi

      Thank you very much. It seems that you were the only one who understood my problem. The strings are by default in hex (they are not numbers generated by me and then placed in hex format). Cheers.

      How is what you are doing any different than what he said he didn't want to do? :)

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

        The OP wants to "convert to decimal" representation, which is the wrong terminology IMO.

        Thinking about numbers in terms of their representational basis is wrong - just because print converts numbers to their representation in decimal, that doesn't mean they are still numbers ;)

Re: Multiply Hex values
by Laurent_R (Canon) on Feb 09, 2017 at 14:53 UTC
    There is a wrong understanding in your post.

    Numbers exist independently of their (hexadecimal, decimal or other) representations.

    From Perldoc: hex interprets EXPR as a hex string and returns the corresponding value.

    The hex function converts a string into a number, it does not convert an hex number into a decimal number (even though if you print the output of hex, it will be printed as a decimal number, this does not imply that they're stored as decimal numbers, as this is irrelevant, they are stored as numbers).

    Now, if you want to multiply numbers, they have some how to be numbers. So you need to convert your strings into numbers (not decimal or hexadecimal numbers), or at least something that Perl understands as numbers. So converting your hexadecimal strings to numbers is the right way to do it. And, once you've done that, you can multiply your numbers, and then print out the resulting number value in any representation you wish, including hexadecimal representation or any other one.

      First, that's exactly the right answer.

      To quibble a little bit, computers don't manipulate numbers in the abstract, they store them in binary. (You can just say "internal representation" if you want to pretend that anyone is using a non-binary computer.)

      Now I'm just getting silly. You could, technically, write code to multiply numbers by directly working on their (decimal or hex) string representations, using the grade school long-form method. It would be a million times slower, because you're emulating something the computer can do in one instruction, but you could do it.

      So, once again, listen to Laurent. Put the numbers in a form the computer can work with easily, let it do its magic, then convert them back to the form you want to look at.

      This is a very common misunderstanding. Common English usage does not distinguish between 'number' (the abstract concept) and 'numeral' (a representation of a number). In the rare cases where it makes a difference, we often get confused. Your use of 'value' solves the problem nicely.
      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1181504]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2019-12-09 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?