在 UNIX shell 脚本中将十进制转换为十六进制
在 UNIX shell 脚本中,我可以使用什么将十进制数转换为十六进制数? 我以为 od 可以解决这个问题,但它没有意识到我正在向它提供数字的 ASCII 表示形式。
打印? 总的! 目前可以使用,但是还有什么可用的呢?
In a UNIX shell script, what can I use to convert decimal numbers into hexadecimal? I thought od would do the trick, but it's not realizing I'm feeding it ASCII representations of numbers.
printf? Gross! Using it for now, but what else is available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果要过滤整个整数文件,每行一个:
If you want to filter a whole file of integers, one per line:
十六进制到十进制:
十进制到十六进制:
Hexidecimal to decimal:
Decimal to hexadecimal:
抱歉我的错,试试这个...
示例:
Sorry my fault, try this...
Example:
尝试:
Try:
就我而言,我偶然发现了使用 printf 解决方案的一个问题:
$ printf "%x" 008
bash: printf: 008: 无效的八进制数
最简单的方法是使用 bc 的解决方案,在更高的帖子中建议:
$ bc <<<< “奥基=16;008”
8
In my case, I stumbled upon one issue with using printf solution:
$ printf "%x" 008
bash: printf: 008: invalid octal number
The easiest way was to use solution with bc, suggested in post higher:
$ bc <<< "obase=16; 008"
8
在
zsh
中,您可以执行此类操作:示例取自
zsh
有关算术评估的文档页面。我相信 Bash 也有类似的功能。
In
zsh
you can do this sort of thing:Example taken from
zsh
docs page about Arithmetic Evaluation.I believe Bash has similar capabilities.
这不是 shell 脚本,但它是我用来在 bin/oct/dec/hex 之间转换数字的 cli 工具:
This is not a shell script, but it is the cli tool I'm using to convert numbers among bin/oct/dec/hex:
对于那些想要使用变量的人,首先通过运行将其导出:
然后运行:
否则,您可以忽略变量的用例并运行直接如下所示:
注意:用您选择的变量名称替换 NUM。
导出使其成为环境变量(全局)。
For those who would like to use variables, first export it by running:
Then run:
Else, you can you can ignore the use case of the variables and run it directly as shown below:
NB:Substitute NUM with the variable name of your choice.
Exporting makes it an environmental variable(global).
也许有人可以帮我将其移植到
bash
,但目前,我只能让这个函数在zsh
中工作,zsh
precision range)BEGIN { _ = substr(__=_=(_=(_=" ")_)_,+_,\n ___ = FS = RS) } ORS = $+_ ___ substr(_, $+_=__)' +baseView:12> awk '!/\43/ ? sub(":: ", "&10#")^_ : /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ ' +baseView:7> __=36 +baseView:9> typeset -i 36 ___=4688888899996789 +baseView:10> echo ' base 36 =:: 36#1A62LTNJ8FP...' +baseView:7> __=35 +baseView:12> rev +baseView:9> typeset -i 35 ___=4688888899996789 +baseView:10> echo ' base 35 =:: 35#1OH7IGLN5MJ...' +baseView:7> __=34 +baseView:9> typeset -i 34 ___=4688888899996789 +baseView:10> echo ' base 34 =:: 34#297MFQOC973...' +baseView:7> __=33 +baseView:9> typeset -i 33 ___=4688888899996789 +baseView:10> echo ' base 33 =:: 33#320V61D8HW4...' +baseView:7> __=32 +baseView:9> typeset -i 32 ___=4688888899996789 +baseView:10> echo ' base 32 =:: 32#458GK2KD33L...' +baseView:7> __=31 +baseView:9> typeset -i 31 ___=4688888899996789 +baseView:10> echo ' base 31 =:: 31#5MAJR5NHNQM...' +baseView:7> __=30 +baseView:9> typeset -i 30 ___=4688888899996789 +baseView:10> echo ' base 30 =:: 30#7S6I6ASBLTJ...' +baseView:7> __=29 +baseView:9> typeset -i 29 ___=4688888899996789 +baseView:10> echo ' base 29 =:: 29#B464H7MRHL9...' +baseView:7> __=28 +baseView:9> typeset -i 28 ___=4688888899996789 +baseView:10> echo ' base 28 =:: 28#FN6R3K2LB05...' +baseView:7> __=27 +baseView:9> typeset -i 27 ___=4688888899996789 +baseView:10> echo ' base 27 =:: 27#MKNQAMAP6QP...' +baseView:7> __=26 +baseView:9> typeset -i 26 ___=4688888899996789 +baseView:10> echo ' base 26 =:: 26#175FBKOCBL57...' +baseView:7> __=25 +baseView:9> typeset -i 25 ___=4688888899996789 +baseView:10> echo ' base 25 =:: 25#1O442DN8OJLE...' +baseView:7> __=24 +baseView:9> typeset -i 24 ___=4688888899996789 +baseView:10> echo ' base 24 =:: 24#31ML285199ND...' +baseView:7> __=23 +baseView:9> typeset -i 23 ___=4688888899996789 +baseView:10> echo ' base 23 =:: 23#4L465B0DB5L2...' +baseView:7> __=22 +baseView:9> typeset -i 22 ___=4688888899996789 +baseView:10> echo ' base 22 =:: 22#80BJ7L8CHI9F...' +baseView:7> __=21 +baseView:9> typeset -i 21 ___=4688888899996789 +baseView:10> echo ' base 21 =:: 21#D826EJ0FH5BJ...' +baseView:7> __=20 +baseView:9> typeset -i 20 ___=4688888899996789 +baseView:10> echo ' base 20 =:: 20#12HHJE914JBJ9...' +baseView:7> __=19 +baseView:9> typeset -i 19 ___=4688888899996789 +baseView:10> echo ' base 19 =:: 19#224EDHE448G89...' +baseView:7> __=18 +baseView:9> typeset -i 18 ___=4688888899996789 +baseView:10> echo ' base 18 =:: 18#40H469ACG9FD7...' +baseView:7> __=17 +baseView:9> typeset -i 17 ___=4688888899996789 +baseView:10> echo ' base 17 =:: 17#80DE6274FD2E3...' +baseView:7> __=16 +baseView:9> typeset -i 16 ___=4688888899996789 +baseView:10> echo ' base 16 =:: 16#10A88505468C75...' +baseView:7> __=15 +baseView:9> typeset -i 15 ___=4688888899996789 +baseView:10> echo ' base 15 =:: 15#26213B62543CE4...' +baseView:7> __=14 +baseView:9> typeset -i 14 ___=4688888899996789 +baseView:10> echo ' base 14 =:: 14#5C9C360DD23205...' +baseView:7> __=13 +baseView:9> typeset -i 13 ___=4688888899996789 +baseView:10> echo ' base 13 =:: 13#1263444313436A7...' +baseView:7> __=12 +baseView:9> typeset -i 12 ___=4688888899996789 +baseView:10> echo ' base 12 =:: 12#379A820B95A33B1...' +baseView:7> __=11 +baseView:9> typeset -i 11 ___=4688888899996789 +baseView:10> echo ' base 11 =:: 11#113902A684A6A784...' +baseView:7> __=10 +baseView:9> typeset -i 10 ___=4688888899996789 +baseView:10> echo ' base 10 =:: 4688888899996789...' +baseView:7> __=9 +baseView:9> typeset -i 9 ___=4688888899996789 +baseView:10> echo ' base 9 =:: 9#24685871741836887...' +baseView:7> __=8 +baseView:9> typeset -i 8 ___=4688888899996789 +baseView:10> echo ' base 8 =:: 8#205210240521506165...' +baseView:7> __=7 +baseView:9> typeset -i 7 ___=4688888899996789 +baseView:10> echo ' base 7 =:: 7#2610433021013104105...' +baseView:7> __=6 +baseView:9> typeset -i 6 ___=4688888899996789 +baseView:10> echo ' base 6 =:: 6#114100233453531122341...' +baseView:7> __=5 +baseView:9> typeset -i 5 ___=4688888899996789 +baseView:12> column '-s#' -t +baseView:10> echo ' base 5 =:: 5#14404040223431344344124...' +baseView:7> __=4 +baseView:9> typeset -i 4 ___=4688888899996789 +baseView:10> echo ' base 4 =:: 4#100222020110011101220301311...' +baseView:7> __=3 +baseView:9> typeset -i 3 ___=4688888899996789 +baseView:10> echo ' base 3 =:: 3#211202212222101211101221020222221...' +baseView:7> __=2 +baseView:9> typeset -i 2 ___=4688888899996789 +baseView:10> echo ' base 2 =:: 2#10000101010001000010100000101010001101000110001110101...' +baseView:12> rev \''#'\'' -t | rev } declare -F baseView baseView 4688888899996789 '
Maybe someone could help me port this to
bash
, but for now, i could only get this function to work inzsh
zsh
precision range)BEGIN { _ = substr(__=_=(_=(_=" ")_)_,+_,\n ___ = FS = RS) } ORS = $+_ ___ substr(_, $+_=__)' +baseView:12> awk '!/\43/ ? sub(":: ", "&10#")^_ : /[ \t][2-9][ \t]/ ? gsub(/[0-9][ \t]/, "0&") : _^_ ' +baseView:7> __=36 +baseView:9> typeset -i 36 ___=4688888899996789 +baseView:10> echo ' base 36 =:: 36#1A62LTNJ8FP...' +baseView:7> __=35 +baseView:12> rev +baseView:9> typeset -i 35 ___=4688888899996789 +baseView:10> echo ' base 35 =:: 35#1OH7IGLN5MJ...' +baseView:7> __=34 +baseView:9> typeset -i 34 ___=4688888899996789 +baseView:10> echo ' base 34 =:: 34#297MFQOC973...' +baseView:7> __=33 +baseView:9> typeset -i 33 ___=4688888899996789 +baseView:10> echo ' base 33 =:: 33#320V61D8HW4...' +baseView:7> __=32 +baseView:9> typeset -i 32 ___=4688888899996789 +baseView:10> echo ' base 32 =:: 32#458GK2KD33L...' +baseView:7> __=31 +baseView:9> typeset -i 31 ___=4688888899996789 +baseView:10> echo ' base 31 =:: 31#5MAJR5NHNQM...' +baseView:7> __=30 +baseView:9> typeset -i 30 ___=4688888899996789 +baseView:10> echo ' base 30 =:: 30#7S6I6ASBLTJ...' +baseView:7> __=29 +baseView:9> typeset -i 29 ___=4688888899996789 +baseView:10> echo ' base 29 =:: 29#B464H7MRHL9...' +baseView:7> __=28 +baseView:9> typeset -i 28 ___=4688888899996789 +baseView:10> echo ' base 28 =:: 28#FN6R3K2LB05...' +baseView:7> __=27 +baseView:9> typeset -i 27 ___=4688888899996789 +baseView:10> echo ' base 27 =:: 27#MKNQAMAP6QP...' +baseView:7> __=26 +baseView:9> typeset -i 26 ___=4688888899996789 +baseView:10> echo ' base 26 =:: 26#175FBKOCBL57...' +baseView:7> __=25 +baseView:9> typeset -i 25 ___=4688888899996789 +baseView:10> echo ' base 25 =:: 25#1O442DN8OJLE...' +baseView:7> __=24 +baseView:9> typeset -i 24 ___=4688888899996789 +baseView:10> echo ' base 24 =:: 24#31ML285199ND...' +baseView:7> __=23 +baseView:9> typeset -i 23 ___=4688888899996789 +baseView:10> echo ' base 23 =:: 23#4L465B0DB5L2...' +baseView:7> __=22 +baseView:9> typeset -i 22 ___=4688888899996789 +baseView:10> echo ' base 22 =:: 22#80BJ7L8CHI9F...' +baseView:7> __=21 +baseView:9> typeset -i 21 ___=4688888899996789 +baseView:10> echo ' base 21 =:: 21#D826EJ0FH5BJ...' +baseView:7> __=20 +baseView:9> typeset -i 20 ___=4688888899996789 +baseView:10> echo ' base 20 =:: 20#12HHJE914JBJ9...' +baseView:7> __=19 +baseView:9> typeset -i 19 ___=4688888899996789 +baseView:10> echo ' base 19 =:: 19#224EDHE448G89...' +baseView:7> __=18 +baseView:9> typeset -i 18 ___=4688888899996789 +baseView:10> echo ' base 18 =:: 18#40H469ACG9FD7...' +baseView:7> __=17 +baseView:9> typeset -i 17 ___=4688888899996789 +baseView:10> echo ' base 17 =:: 17#80DE6274FD2E3...' +baseView:7> __=16 +baseView:9> typeset -i 16 ___=4688888899996789 +baseView:10> echo ' base 16 =:: 16#10A88505468C75...' +baseView:7> __=15 +baseView:9> typeset -i 15 ___=4688888899996789 +baseView:10> echo ' base 15 =:: 15#26213B62543CE4...' +baseView:7> __=14 +baseView:9> typeset -i 14 ___=4688888899996789 +baseView:10> echo ' base 14 =:: 14#5C9C360DD23205...' +baseView:7> __=13 +baseView:9> typeset -i 13 ___=4688888899996789 +baseView:10> echo ' base 13 =:: 13#1263444313436A7...' +baseView:7> __=12 +baseView:9> typeset -i 12 ___=4688888899996789 +baseView:10> echo ' base 12 =:: 12#379A820B95A33B1...' +baseView:7> __=11 +baseView:9> typeset -i 11 ___=4688888899996789 +baseView:10> echo ' base 11 =:: 11#113902A684A6A784...' +baseView:7> __=10 +baseView:9> typeset -i 10 ___=4688888899996789 +baseView:10> echo ' base 10 =:: 4688888899996789...' +baseView:7> __=9 +baseView:9> typeset -i 9 ___=4688888899996789 +baseView:10> echo ' base 9 =:: 9#24685871741836887...' +baseView:7> __=8 +baseView:9> typeset -i 8 ___=4688888899996789 +baseView:10> echo ' base 8 =:: 8#205210240521506165...' +baseView:7> __=7 +baseView:9> typeset -i 7 ___=4688888899996789 +baseView:10> echo ' base 7 =:: 7#2610433021013104105...' +baseView:7> __=6 +baseView:9> typeset -i 6 ___=4688888899996789 +baseView:10> echo ' base 6 =:: 6#114100233453531122341...' +baseView:7> __=5 +baseView:9> typeset -i 5 ___=4688888899996789 +baseView:12> column '-s#' -t +baseView:10> echo ' base 5 =:: 5#14404040223431344344124...' +baseView:7> __=4 +baseView:9> typeset -i 4 ___=4688888899996789 +baseView:10> echo ' base 4 =:: 4#100222020110011101220301311...' +baseView:7> __=3 +baseView:9> typeset -i 3 ___=4688888899996789 +baseView:10> echo ' base 3 =:: 3#211202212222101211101221020222221...' +baseView:7> __=2 +baseView:9> typeset -i 2 ___=4688888899996789 +baseView:10> echo ' base 2 =:: 2#10000101010001000010100000101010001101000110001110101...' +baseView:12> rev \''#'\'' -t | rev } declare -F baseView baseView 4688888899996789 '
哇,我没有意识到 printf 在 shell 中可用!
话虽如此,我很惊讶没有人评论将 printf 放入 shell 脚本中(如果需要,您可以将其放入您的个人 bin 目录中)。
echo "printf "0x%x\n" $1" > 十六进制
chmod +x hex
现在运行:
./hex 123
它返回:
0x7b
Wow, I didn't realize that printf was available at the shell!
With that said, I'm surprised no-one commented about putting the printf into a shell script (which then you could put in your personal bin directory if you wanted).
echo "printf "0x%x\n" $1" > hex
chmod +x hex
Now just run:
./hex 123
It returns:
0x7b
尝试过 printf(1) 吗?
可能有一些方法可以使用所有 shell 中的内置函数来做到这一点,但它的可移植性较差。 我还没有检查 POSIX sh 规范来看看它是否具有这样的功能。
Tried
printf(1)
?There are probably ways of doing that with builtin functions in all shells but it would be less portable. I've not checked the POSIX sh specs to see whether it has such capabilities.