math
[!NOTE|label:references:]
sum
[!INFO|label:references:]
awk
file sizes
datamash
[!TIP|label:reference:]
bc
sum from file
[!NOTE|label:sample file:]
Σn where 1<=n<=100000
number conversion
[!NOTE|label:references:]
tips
ibaseandobaseparams order matters, but not always. Hex values must be in UPPERCASE.
the decimal can be ignored in
ibaseorobase, or we can say, the defaultibaseandobaseare all 10.
八进制 → 二进制
echo "ibase=8; obase=2; 77" | bc
111111
十进制 → 十六进制
echo "obase=16; 255" | bc
FF
十六进制 → 十进制
echo "ibase=16; FF" | bc
255
二进制 → 十进制
echo "ibase=2; 101010" | bc
42
supported base keywords
bin
2
Binary
oct
8
Octal
dec
10
Decimal
hex
16
Hexadecimal
Common Bases
Base → Decimal
$((base#value))
echo $((2#1010)) → 10
Decimal → Hex
printf
printf "%X\n" 255 → FF
Decimal → Binary
bc
bc < <(echo "obase=2; 42") → 101010
Octal → Decimal
$((8#17))
$((017))
echo $((8#17)) → 15
Hex → Decimal
$((16#FF))
$((0xFF))
echo $((16#FF)) → 255
base → decimal
2
10
$((2#1011))
1011
11
BINARY
8
10
$((8#17))
17
15
OCTAL
8
10
$((017))
17
15
OCTAL
10
10
$((10#42))
42
42
DECIMAL
16
10
$((16#1A3F))
1A3F
6719
HEXADECIMAL
16
10
$((0x1A3F))
1A3F
6719
HEXADECIMAL
36
10
$((36#Z))
Z
35
ALPHANUMERIC MAX
64
10
$((64#_))
_
63
MAX BASE IN BASH
binary <> decimal <> hexadecimal
[!NOTE]
obase:[o]utput base
ibase:[i]utput base
to decimal
Binary
Decimal
echo $((2#1010))
2#1010
10
-
Hex
Decimal
echo $((16#FF))
16#FF
255
-
Hex
Decimal
echo $((0xFF))
16#FF
255
0x - convert from hex
Octal
Decimal
echo $((8#77))
8#77
63
-
Octal
Decimal
echo $((077))
8#77
63
0 - convert from octal
from hexadecimal
from octal
to hexadecimal
[!TIP]
if convert from decimal, the
ibasecan be ignored, or we can say, the defaultibaseis 10.
Binary
Hex
echo "obase=16; ibase=2; 101011" | bc
101011 → hex
2B
Octal
Hex
echo "obase=16; ibase=8; 77" | bc
77 (octal) → hex
3F
Decimal
Hex
echo "obase=16; 255" | bc
255
FF
Decimal
Hex
printf "%X\n" 255
255
FF
from decimal
from octal
to octal
Binary
Octal
echo "obase=8; ibase=2; 1010" | bc
1010 → base 2 → base 8
12
Decimal
Octal
printf "%o\n" 42
42
52
Hex
Octal
echo "obase=8; ibase=16; FF" | bc
FF → hex to octal
377
from hexadecimal
from decimal
to binary
[!NOTE|label:references:]
Octal
Binary
echo "obase=2; ibase=8; 77" | bc
8#77 → obase=2
111111
Decimal
Binary
echo "obase=2; 42" | bc`
42
101010
Hex
Binary
echo "obase=2; ibase=16; FF" | bc
ibase=16; FF → obase=2
11111111
from decimal
to unicode ( hexadecimal )
[!NOTE|label:references:]
more
to hexadecimal
to octal
number converting from file
octal
hexadecimal
advanced computing
[!NOTE|label:references:]
logarithm
bc
awk
power
bc
$(())
square
Last updated
Was this helpful?