awk输出到BC HexToDec计算中 [英] awk output into BC HexToDec calculation
问题描述
我正在尝试组合awk
和bc
以便创建bash函数,以将十六进制值快速转换为正确的格式.
I am trying to combine awk
and bc
in order to create a bash function for quickly converting hex values into a proper format.
echo FFFFFF | VAR=$(awk '{print toupper($0)}') | "ibase=16; $((((($VAR - 5) / 100) - 1000)))" | bc
我正在尝试这样做,但似乎无法将awk
的输出输入到bc
参数的输入中.
I am trying this but I cant seem to get the output of awk
into the input of the bc
argument.
推荐答案
尽管您尝试使用awk
和bc
进行操作,但我在这里为您提供了最方便的选择:printf
.这使您可以轻松地在十进制,八进制和十六进制之间进行转换.
Although you try to do it with awk
and bc
, I give you the most convenient option here : printf
. This allows you to easily convert between decimal, octal and hexadecimal.
[bash] % printf "%d" 0xFFFFFF # hex2dec
16777215
[bash] % printf "%#O" 0xFFFFFF # hex2oct
077777777
[bash] % printf "%d" 077777777 # oct2dec
16777215
[bash] % printf "%d" 077777777 # oct2hex
0xFFFFFF
[bash] % printf "%#x" 16777215 # dec2hex
0xFFFFFF
[bash] % printf "%#O" 16777215 # dec2oct
077777777
额外的字符#
要求printf为十六进制数字插入前缀0X
,为八进制数字插入0
.如果您以十六进制形式添加数字,请不要忘记在数字前添加前缀0x
,在八进制数字前添加0
.
The extra character #
asks printf to insert the prefix 0X
for hexadecimal numbers, and 0
for octal numbers. If you add numbers in hexadecimal form, do not forget to add the prefix 0x
to the number, and 0
for octal numbers.
当然bash
可以进行算术运算.
Ofcourse bash
has an option to do arithmatic operations.
man bash
((expression))
根据以下算术评估中所述的规则评估表达.如果表达式的值不为零,则返回状态为0
;否则,返回状态为0
.否则返回状态为1
.这完全等同于let "expression"
.
man bash
((expression))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is0
; otherwise the return status is1
. This is exactly equivalent tolet "expression"
.
这使您可以直接进行所有计算.例如.您想要0xFFFFFF
并减去小数5
然后除以100
(十进制)并减去1000小数,您就有了
This allows you to do all your computations directly. Eg. you want 0xFFFFFF
and subtract decimal 5
and divide by 100
(decimal) and subtract 1000 decimal, you have
[bash] % printf "%d" $(( (0xFFFFFF - 5)/100 - 1000 ))
166772
或者您可以全部使用十六进制::
Or you can do it all hexadecimal ::
[bash] % printf "%d" $(( (0xFFFFFF - 0x5)/0x100 - 0x1000 ))
61439
如果您现在想将这样的数字存储在变量中,则只需
If you now want to store such a number in a variable, you just do
[bash] % vardec=$( printf "%d" $(( (0xFFFFFF - 0x5)/0x100 - 0x1000 )) )
[bash] % varhex=$( printf "%#x" $(( (0xFFFFFF - 0x5)/0x100 - 0x1000 )) )
[bash] % varoct=$( printf "%#O" $(( (0xFFFFFF - 0x5)/0x100 - 0x1000 )) )
但是,十进制是一种特殊情况,您可以摆脱printf
:
However, decimal is such a special case, that you can get rid of the printf
:
[bash] % vardec=$(( (0xFFFFFF - 0x5)/0x100 - 0x1000 ))
bash
的算术评估系统允许您以任何基数(最高为64)写任何数字,并将其转换为十进制.在这里,我将以3为底的数字3#1212
转换为数字50.
The arithmetic evaluation system of bash
allows you to write any number in any base (up to base 64), and convert it to decimal. Here I convert a base 3 number 3#1212
into the number 50.
[bash] % echo $(( 3#1212 ))
50
如果您想使用 zsh
,则可以转换回任何其他基准:
If you wan to use zsh
, you can convert back to any other base :
[zsh] % echo $(( [##2]7 )) # from base 10 to base 2
111
[zsh] % echo $(( [##5]3#1020101100 )) # from base 3 to base 5
1234321
[zsh] % echo $(( [##36]17#415C67A8 )) # from base 17 to base 36
ROFLOL
这篇关于awk输出到BC HexToDec计算中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!