AWK、printf 格式说明符 %x 存在负值问题
似乎 AWK 在无符号十六进制格式说明符方面存在问题:
echo 0x80000000 | awk '{printf("0x%08x\n", $1)}'
返回: 0x7fffffff
这是 awk 的已知问题吗?
谢谢!
It seems AWK has problems with the unsigned hex format specifier:
echo 0x80000000 | awk '{printf("0x%08x\n", $1)}'
gives back: 0x7fffffff
Is this a known problem with awk?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 awk 仅在输入参数为十进制时自动将其转换为数字。但这应该有效:
echo 0x80000000 | awk '{printf("0x%08x\n", strtonum($1))}'
这一切都在此处的 strtonum 部分进行了解释:
http://www.gnu.org/manual/gawk /html_node/String-Functions.html#String-Functions
The problem is that awk only converts input parameters to numbers automatically if they are decimal. But this should work:
echo 0x80000000 | awk '{printf("0x%08x\n", strtonum($1))}'
It's all explained in here, in the strtonum section:
http://www.gnu.org/manual/gawk/html_node/String-Functions.html#String-Functions
在这里没有看到它,虽然我无法像您一样使用十六进制输入,但转换为十进制没有问题。
如果您愿意告诉我们您所在的平台(这是 GNU awk 3.1.5),我们也许可以为您提供更多帮助。
Not seeing it here, although I wasn't able to use the hex input as you are, but converted to decimal was no problem.
If you care to enlighten us what platform you're on (this was GNU awk 3.1.5), we might be able to help you more.