On 04/12/2012 04:43 PM, Shawn H Corey wrote:
On 12-04-12 04:34 PM, Parag Kalra wrote:
Why does the output of
perl -e "print hex '0x00001600000402'"

differs from the output of
perl -e "print hex 0x00001600000402"

EG:
bash-3.2$ perl -e "print hex '0x00001600000402'"
94489281538

$ perl -e "print hex 0x00001600000402"
10189963531576


$ perl -e "print hex '0x16'"
22
$ perl -e "print hex 0x16"
34
$ perl -e "print hex '22'"
34

When inside quotes, '0x16' is interpreted by hex to be a hex string and
convert to 22. When outside, perl interprets 0x16 as 22 which is passed
to hex to produce 34.

shawn has it correct here but i want to clarify things a bit. perl will see any literal beginning with 0x as a hex number and always convert that to an internal integer (no base is used there, but think decimal for your brain's sake :). the hex function always takes a string so any argument to it will be converted to a base 10 string. as shawn showed, that will make 22 decimal become a string and then be converted to 34 decimal.

the lesson is to be aware of the difference in literals and strings and what is doing the parsing and conversion, perl or the hex function.

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to