shell 进制 转化

How to convert Decimal to Octal, binary, Hexadecimal from command line

when you work at routing, you will find yourself converting decimal to binary and the opposite, specially for the masks of the IP address, here I will show you how to do it easily using your command line terminal.

Decimal to Hexadecimal

echo 'obase=16;10'| bc

A
Or

wcalc -h 10

Decimal to Octal

echo 'obase=8;10' | bc

12
Or

wcalc -o 10

Decimal to Binary

echo 'obase=2;10' | bc

1010
Or

wcalc -b 10

From Hexadecimal to decimal

echo 'ibase=16;A' | bc

10
From Octal to Decimal

echo 'ibase=8;12 | bc

10
From Binary to Decimal

echo 'ibase=2;1010 | bc

10
Note: Be sure to have bc or wcalc installed on your system



If you have a value in hex format and want to see its decimal value, you can easily convert it using the terminal. To convert 3f hexadecimal type (in a bash shell):
 $ let x=0x3f
 $ echo $x
 63

 $ let x=0xfffe
 $ echo $x
 65534

isntead of two commands, you can do it using arithmetic expression interpolation:

echo $[0xfffe]

printf is a bit more flexible, you can convert from hex back into decimal,
etc. It works just about like the C library function, only you run it from
the shell.

$ printf "%02X\n", 123
7B
$ printf "%d\n", 0x7B
123



Here's how you could do it as a Perl one-liner:
perl -e 'print 0x3f, "\n"'

or

perl -e 'printf("%d\n", 0x3f)'


Two bash aliases that might help out...

alias h2d='printf "%d\n" ${1}'
alias d2h='printf "0x%x\n" ${1}'


Hex, Octal and Binary shell conversions
Hex, Octal and Binary shell conversions


 

In scripting, it is common to find a need hexadecimal (hex or base 16) numbers, and occasionally binary numbers. Converting can be tricky without some tips. You can use printf, typeset and even bc as conversion tools. Here are some ideas:

HEX conversions


Shell:

HEX=1fa
echo $((16#$HEX))
506    or: DECIMAL=$((16#$HEX))



typeset (shell):
typeset -i16 HEX=506 (convert decimal to hex)
echo $HEX
16#1fa (typeset adds the base 16# and uses lowercase hex chars)


printf:

printf “%d\n” 0x1fa (printf understands 0x for hex numbers)

506

and decimal to hex for printf:

printf “%X %x\n” 506 506 (printf uses X and x for UPPER and lower case)

1FA 1fa
or:

printf “0x%X 0x%x\n” 506 506 (add the 0x to flag as hex)

0x1FA 0x1fa


bc:
HEX=1FA         (mandatory UPPERCASE for bc)

echo “ibase=16\n$HEX” | bc
506


OCTAL conversions


Shell:

OCTAL=772
echo $((8#$OCTAL))
506    or: DECIMAL=$((8#$OCTAL))



typeset (shell):
typeset -i8 OCTAL=506 (convert decimal to octal)
echo $OCTAL
8#772 (typeset adds the base 8#)


printf:

printf “%d\n” 0772 (printf understands 0 prefix for octal numbers)

506

and decimal to octal for printf:

printf “%o\n” 506

772
or:

printf “0%o\n” 506 (add the leading 0 to flag as octal)

0772


bc:
OCTAL=772
echo “ibase=8\n$OCTAL” | bc
506

 


BINARY conversions


Shell:

BINARY=111111010
echo $((2#$BINARY))
506    or: DECIMAL=$((8#$BINARY))



typeset (shell):
typeset -i2 BINARY=506 (convert decimal to octal)
echo $BINARY
2#111111010 (typeset adds the base 2#)


bc:
BINARY=111111010
echo “ibase=2\n$BINARY” | bc
506



一个关于在SHELL中逐行读取文件内容并将其赋给变量的问题
gawk '$1 == "pattern" {print $2,$3,$4}'|bash dealscript
dealscrit内容测试如下:
#!/bin/bash
while read var1 var2 var3; do
echo var1=${var1} var2=${var2}
done
    


%!xxd
%!xxd -r
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值