Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Hex Question(s)

by pc88mxer (Vicar)
on Mar 27, 2008 at 16:20 UTC ( #676778=note: print w/replies, xml ) Need Help??


in reply to Hex Question(s)

Is the hex() function what you are looking for?
my $theHex = "100"; print hex($theHex), "\n"; # prints: 256

Replies are listed 'Best First'.
Re^2: Hex Question(s)
by deadlock (Initiate) on Mar 27, 2008 at 16:41 UTC

    No, that's converting a hex value to an integer. That's not what I'm trying to do - I'm actually doing the opposite: I'm converting an integer to hex but Perl doesn't appear to be treating the resulting string as hex.

    Put it this way.

    print 0x1234+0x4321; # returns 21605 print '0x1234'+'0x1234'; returns 0 because you can't concatenate strin +gs in this way

      No, Perl's treating it like any other hex string in that it's not a valid number. Perl understands hex numeric literals, but they're silently converted to decimal under the hood. If you want to do manipulation of numbers then you need to use hex to parse the number to decimal and then only reformat as hex on the way out after you're done manipulating it.

      Update: Ooh, better said here up above.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      Are you sure that hex isn't what you're looking for?
      perl -le "print hex('1234') + hex('4321')" 21845 perl -le "print hex('0x1234') + hex('0x4321')" 21845

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://676778]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2020-02-18 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (79 votes). Check out past polls.

    Notices?