如何使与awk的十六进制数计算? [英] How to make calculations on hexadecimal numbers with awk?

查看:764
本文介绍了如何使与awk的十六进制数计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含十六进制数字列表的文件,如为0x12345678 每行一个。

I have a file containing a list of hexadecimal numbers, as 0x12345678 one per line.

我想就这些计算。对于这一点,我想用 AWK 的。但是,如果打印与十六进制数 AWK 易与的printf 功能,我还没有找到一个方法来间preT不是文本等十六进制输入(或 0 ,转换为整数停在 X )。

I want to make a calculation on them. For this, I thought of using awk. But if printing an hexadecimal number with awk is easy with the printf function, I haven't find a way to interpret the hexadecimal input other than as text (or 0, conversion to integer stops on the x).

awk '{ print $1; }'                     // 0x12345678
awk '{ printf("%x\n", $1)}'             // 0
awk '{ printf("%x\n", $1+1)}'           // 1                 // DarkDust answer
awk '{ printf("%s: %x\n", $1, $1)}'     // 0x12345678: 0

是否可以打印,例如值+1?

Is it possible to print, e.g. the value +1?

awk '{ printf(%x\n", ??????)}'          // 0x12345679

修改的:对其他语言的单行欢迎! (如果合理的长度;-))

Edit: One liners on other languages welcomed! (if reasonable length ;-) )

推荐答案

在原始的 NAWK mawk 实施十六进制(和八进制)数的认可。 GAWK (我猜你正在使用)具有不这样做的特性/错误。它有一个命令行开关来得到你想要的行为: - 非十进制数据

In the original nawk and mawk implementations the hexadecimal (and octal) numbers are recognised. gawk (which I guess you are using) has the feature/bug of not doing this. It has a command line switch to get the behaviour you want: --non-decimal-data.

echo 0x12345678 | mawk '{ printf "%s: %x\n", $1, $1 }'
0x12345678: 12345678

echo 0x12345678 | gawk '{ printf "%s: %x\n", $1, $1 }'
0x12345678: 0

echo 0x12345678 | gawk --non-decimal-data '{ printf "%s: %x\n", $1, $1 }'
0x12345678: 12345678

这篇关于如何使与awk的十六进制数计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆