十六进制到十二进制转换(带sed的管道) [英] Hex to Dec conversion (pipe with sed)
本文介绍了十六进制到十二进制转换(带sed的管道)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 sed
之后使用管道将我的十六进制值转换为十进制值。
从'litte endian'到'big endian'
dec_value =`echo dede0a01 | sed's,\(.. \)\(.. \)\(.. \)\(.. \),\ 4\3\2\1, g'`
更新:
这工作正常:
endi =`echo dede0a01 | sed's,\(.. \)\(.. \)\(.. \)\(.. \),\ 4\3\2\1, g'| tr'[a-z]''[A-Z]'`
echoibase = 16; $ endi| bc
但是我很好奇这是否可以用一行?
解决方案 在将它们全部输送到
bc $之前,将 sed
添加 ibase = 16;
c code
$ b $ $ $ $ $ $ $ $ c $ dec_value = $(echo dede0a01 | tr'[az]''[AZ]'| sed' S,\(.. \)\(.. \)\(.. \)\(.. \),基准值= 16; \4\3\2\ 1,g'| bc)
如果您使用Bash,ksh或zsh, t需要 tr
和 bc
:
<(code>)((dec_value = 16#$(echo dede0a01 | sed's,\(.. \)\(.. \)\(.. \)\(..) \4\3\2\1,g')))
或没有 echo 和
sed
,:
<$ p $(十六进制:6:2)$ {十六进制:4:2} $ {十六进制:2:2} $ {hex:dede0a01
((dec_value = 16# 0:2}) )
How can I convert my Hex value to Dec value using pipe after sed
.
Conversion from 'litte endian' to 'big endian'
dec_value=`echo dede0a01 | sed 's,\(..\)\(..\)\(..\)\(..\),\4\3\2\1,g'`
Update:
This works fine:
endi=`echo dede0a01 | sed 's,\(..\)\(..\)\(..\)\(..\),\4\3\2\1,g' | tr '[a-z]' '[A-Z]'`
echo "ibase=16; $endi" | bc
But I'm curious whether this is possible with one line?
解决方案
Do your tr
before the sed
and have sed
add the ibase=16;
before piping it all into bc
:
dec_value=$(echo dede0a01 | tr '[a-z]' '[A-Z]' | sed 's,\(..\)\(..\)\(..\)\(..\),ibase=16;\4\3\2\1,g' | bc)
If you're using Bash, ksh or zsh, you don't need tr
and bc
:
(( dec_value = 16#$(echo dede0a01 | sed 's,\(..\)\(..\)\(..\)\(..\),\4\3\2\1,g') ))
or without echo
and sed
, too:
hex=dede0a01
(( dec_value = 16#${hex:6:2}${hex:4:2}${hex:2:2}${hex:0:2} ))
这篇关于十六进制到十二进制转换(带sed的管道)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文