math
Last updated
Was this helpful?
Last updated
Was this helpful?
[!NOTE|label:references:]
[!INFO|label:references:]
file sizes
[!TIP|label:reference:]
[!NOTE|label:sample file:]
Σn where 1<=n<=100000
[!NOTE|label:references:]
tips
ibase
andobase
params order matters, but not always. Hex values must be in UPPERCASE.
the decimal can be ignored in
ibase
orobase
, or we can say, the defaultibase
andobase
are 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
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
[!NOTE]
obase
:[o]utput base
ibase
:[i]utput base
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
[!TIP]
if convert from decimal, the
ibase
can be ignored, or we can say, the defaultibase
is 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
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
[!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
[!NOTE|label:references:]
more
to hexadecimal
to octal
octal
hexadecimal
[!NOTE|label:references:]
bc
awk
bc
$(())