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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.