Системы счисления

Простые типы / Числа / Integer

#decimal number system
decimal=42

#octal number system: a leading zero
octal=$((042))
#octal is 34

#hexadecimal number system
hexadecimal=$((0x42))
#hexadecimal is 66

#any base from 2 to 64: base#number
binary=$((2#1010))
#binary is 10

#the same form reads a string in any base back
fromHexadecimal=$((16#2a))
#fromHexadecimal is 42

#42 to octal and hexadecimal string
printf -v sOctal '%o' 42
#sOctal is "52"
printf -v sHexadecimal '%x' 42
#sHexadecimal is "2a"
printf -v sPrefixed '%#x' 42
#sPrefixed is "0x2a"

#printf has no binary format, bc prints a number in any base
sBinary=$(bc <<< "obase=2; 42")
#sBinary is "101010"

echo "decimal is $decimal"
echo "octal is $octal"
echo "hexadecimal is $hexadecimal"
echo "binary is $binary"
echo "fromHexadecimal is $fromHexadecimal"
echo "sOctal is $sOctal"
echo "sHexadecimal is $sHexadecimal"
echo "sPrefixed is $sPrefixed"
echo "sBinary is $sBinary"