如何用awk进行十六进制数的计算?

发布于 2024-09-19 03:14:38 字数 629 浏览 7 评论 0原文

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

我想对他们算一笔账。为此,我想到了使用awk。但是,如果使用 printf 函数使用 awk 打印十六进制数字很容易,那么我还没有找到一种方法来将十六进制输入解释为文本(或 0,到整数的转换在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?

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

编辑:欢迎使用其他语言的衬里! (如果长度合理;-))

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

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

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 ;-) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

厌味 2024-09-26 03:14:38

在原始的 nawkmawk 实现中,可以识别十六进制(和八进制)数字。 gawk (我猜你正在使用它)具有不这样做的功能/错误。它有一个命令行开关来获取您想要的行为:--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

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
罗罗贝儿 2024-09-26 03:14:38

gawk 有 strtonum 函数:

% echo 0x12345678 | gawk '{ printf "%s: %x - %x\n", $1, $1, strtonum($1) }'
0x12345678: 0 - 12345678

gawk has the strtonum function:

% echo 0x12345678 | gawk '{ printf "%s: %x - %x\n", $1, $1, strtonum($1) }'
0x12345678: 0 - 12345678
北城半夏 2024-09-26 03:14:38

也许您根本不需要 awk,因为字符串/数字转换非常复杂。 Bash 版本 3 和 4 非常强大。留在 Bash 中通常更简单、更清晰、更便携,并且可能使用 grepcut 等。

例如,在 Bash 十六进制数字自然转换:

$ printf "%d" 0xDeadBeef
3735928559
$ x='0xE'; printf "%d %d %d" $x "$x" $((x + 1))
14 14 15

希望这有帮助。

Maybe you don't need awk at all, as string/number conversion is hairy. Bash versions 3 and 4 are very powerful. It is often simpler, clearer and more portable to stay in Bash, and maybe use grep and cut etc.

For example, in Bash hexadecimal numbers are converted naturally:

$ printf "%d" 0xDeadBeef
3735928559
$ x='0xE'; printf "%d %d %d" $x "$x" $((x + 1))
14 14 15

Hope this helps.

橘香 2024-09-26 03:14:38

以下是它们行为的不同组合:

使用此命令

'BEGIN { print 0xFACECAFEFEED^2, -0xFEEDCAFEFACEBEEFDEADFACEFEED7,
 -"0xCAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE" }'

gawk -e (GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1))

  76046928626116243263483543552 -82729151009071240233065844435845120 0

gawk -P -e

  00 0
  -21377898657284658184582485743897013874545437686817998522919218577408

gawk -c -e

  00 0 0 

gawk -n -e

  76046928626116243263483543552 -82729151009071240233065844435845120
  -21377898657284658184582485743897013874545437686817998522919218577408

gawk -S -e

  76046928626116243263483543552 -82729151009071240233065844435845120 0

gawk -M

76046928626116245157029816169
-82729151009071239007500567260950231 0

gawk -l mpfr

  76046928626116243263483543552 -82729151009071240233065844435845120 0

nawk (macos awk version 20200816)

 00 0 -2.13778986572846581845824857439e+67

mawk 1.3.4

 00 0 -2.13779e+67

mawk2-beta (1.9.9.6)

 00 0 0

事实上,如果有一个自定义 awk 脚本库,可以跨多个 awk 变体工作,但也想考虑它们的特性,一种方法是使用此处输出的差异来自动标记,在需要时留下相对较少的组合决胜局。

here are the different combinations of their behaviors :

using this command

'BEGIN { print 0xFACECAFEFEED^2, -0xFEEDCAFEFACEBEEFDEADFACEFEED7,
 -"0xCAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE" }'

gawk -e (GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1))

  76046928626116243263483543552 -82729151009071240233065844435845120 0

gawk -P -e

  00 0
  -21377898657284658184582485743897013874545437686817998522919218577408

gawk -c -e

  00 0 0 

gawk -n -e

  76046928626116243263483543552 -82729151009071240233065844435845120
  -21377898657284658184582485743897013874545437686817998522919218577408

gawk -S -e

  76046928626116243263483543552 -82729151009071240233065844435845120 0

gawk -M

76046928626116245157029816169
-82729151009071239007500567260950231 0

gawk -l mpfr

  76046928626116243263483543552 -82729151009071240233065844435845120 0

nawk (macos awk version 20200816)

 00 0 -2.13778986572846581845824857439e+67

mawk 1.3.4

 00 0 -2.13779e+67

mawk2-beta (1.9.9.6)

 00 0 0

In fact, if one has a custom awk-script library that works across multiple awk variants, but also wanna take their idiosyncrasies into account, one approach would be use the difference in outputs here to auto-flag, with relatively few combinations left where one needs a tie-breaker.

狼亦尘 2024-09-26 03:14:38

*** 这只是我在 schot 的回复后发表的评论的延伸,严格来说是为了正确的格式化目的。

echo FACEBEACEBEFACEEFFFACEEEFFFACEFACFACEB | 
    mawk '{ printf("%s\n%.f\n%x\n%.f\n",$0,$0,"0x"$0,"0x"$0) }'

FACEBEACEBEFACEEFFFACEEEFFFACEFACFACEB
0
ffffffff
5593196314036579851314282024549245003233230848

5593196314036579608368524797845507287542639851 #exact

*** this is only an extension of my comment following schot's response, strictly for proper formatting purposes.

echo FACEBEACEBEFACEEFFFACEEEFFFACEFACFACEB | 
    mawk '{ printf("%s\n%.f\n%x\n%.f\n",$0,$0,"0x"$0,"0x"$0) }'

FACEBEACEBEFACEEFFFACEEEFFFACEFACFACEB
0
ffffffff
5593196314036579851314282024549245003233230848

5593196314036579608368524797845507287542639851 #exact
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文