传递字符串“0x30”和“0x30”之间的区别和十六进制数 0x30 到 hex() 函数
print hex("0x30");
给出正确的十六进制到十进制的转换。
什么是 print hex(0x30);
是什么意思? 它给出的值为 72。
print hex("0x30");
gives the correct hex to decimal conversion.
What doesprint hex(0x30);
mean?
The value it's giving is 72.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
hex() 接受一个字符串参数,因此由于 Perl 的弱类型,无论您传递什么参数,它都会将参数读取为字符串。
前者将
0x30
作为字符串传递,然后hex()
直接转换为十进制。后者是一个十六进制数
0x30
,十进制数为 48,被传递给hex()
,然后再次解释为十六进制数并转换为十进制数 72。就像执行hex(hex("0x30"))
一样。您应该坚持使用
hex("0x30")
。hex()
takes a string argument, so due to Perl's weak typing it will read the argument as a string whatever you pass it.The former is passing
0x30
as a string, whichhex()
then directly converts to decimal.The latter is a hex number
0x30
, which is 48 in decimal, is passed tohex()
which is then interpreted as hex again and converted to decimal number 72. Think of it as doinghex(hex("0x30"))
.You should stick with
hex("0x30")
.要扩展 marcog 的答案:来自
perldoc -f hex
所以hex实际上是用于字符串和十六进制值之间的转换。通过输入 0x30,您已经创建了一个十六进制值。
给出
To expand on marcog's answer: From
perldoc -f hex
So hex really is for conversion between string and hex value. By typing in 0x30 you have already created a hex value.
gives
hex()
解析十六进制字符串并返回适当的整数。因此,当您执行
hex(0x30)
时,您的数字文字 (0x30) 会被解释为这样(0x30 是十六进制格式的 48),然后 hex() 将该标量值视为字符串(“48”)并将其转换为数字,假设字符串是十六进制格式。 0x48 == 72,这就是 72 的来源。hex()
parses a hex string and returns the appropriate integer.So when you do
hex(0x30)
your numeric literal (0x30) gets interpreted as such (0x30 is 48 in hex format), then hex() treats that scalar value as a string ("48") and converts it into a number, assuming the string is in hex format. 0x48 == 72, which is where the 72 is coming from.