in reply to hex numbers
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: hex numbers
by shmem (Chancellor) on Jan 14, 2021 at 18:06 UTC | |
by davido (Cardinal) on Jan 14, 2021 at 19:14 UTC | |
by shmem (Chancellor) on Jan 14, 2021 at 19:50 UTC | |
by davido (Cardinal) on Jan 14, 2021 at 20:17 UTC | |
by haukex (Archbishop) on Jan 14, 2021 at 18:17 UTC | |
by shmem (Chancellor) on Jan 14, 2021 at 18:36 UTC | |
by dave_the_m (Monsignor) on Jan 14, 2021 at 19:06 UTC | |
by jcb (Parson) on Jan 15, 2021 at 02:34 UTC | |
by shmem (Chancellor) on Jan 14, 2021 at 19:58 UTC | |
by haukex (Archbishop) on Jan 14, 2021 at 19:04 UTC |
In Section
Seekers of Perl Wisdom