Difference between passing string "0x30" and hexadecimal number 0x30 in hex ()

print hex("0x30"); gives the correct hexadecimal conversion with decimal precision.

What print hex(0x30);does it mean? The value that he gives is 72.

+5
source share
3 answers

hex() takes a string argument, so due to weak Perl input, it will read the argument as the string you pass.

The first passes 0x30as a string, which is hex()then converted directly to decimal.

0x30, 48 , hex(), 72. hex(hex("0x30")).

hex("0x30").

$ perl -e 'print 0x30';
48
$ perl -e 'print hex(0x30)';
72
$ perl -e 'print hex(30)';
48
$ perl -e 'print hex("0x30")';
48
$ perl -e 'print hex(hex(30))';
72
+13

marcog: perldoc -f hex

hex EXPR: EXPR .

hex . 0x30, .

perl -E '
  say 0x30;
  say hex("0x30");
  say 0x48;
  say hex(0x30);
  say hex(hex("0x30"));'

48
48
72
72
72
+3

hex() .

So, when you do hex(0x30), your numeric literal (0x30) is interpreted as such (0x30 - 48 in hexadecimal format), then hex () treats this scalar value as a string ("48") and converts it to number, assuming the string is in hexadecimal format. 0x48 == 72, from which 72 comes.

+2
source

All Articles