The 0xNNNN notation is a source-code feature. It is parsed at compile-time. So this works:

my $n = 0xDEADBEEF; print "$n\n"; # Output: # 3735928559

And this works but you shouldn't use it:

# This works but is NOT recommended, and can be unsafe. my $n = eval "0xDEADBEEF"; print "$\n"; # Output: # 3735928559

That second example works because even though we've created a string of characters, "0xDEADBEEF", the eval statement evaluates that string as source code, so it gets parsed and compiled when the statement is executed at runtime.

This doesn't work:

my $n = "0xDEADBEEF"; print "$n\n"; # Output: # 0xDEADBEEF

It didn't work because the string was taken as plain old characters. Since you want to take a string of characters and interpret them as a hex digits you should use the hex function:

This is the version you should use:

my $string = "DEADBEEF"; my $n = hex($string); print "$n\n"; # Output: # 3735928559

Dave


In reply to Re: hex numbers by davido
in thread hex numbers by LloydRice

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.