SoFunction
Updated on 2025-03-04

How to convert each other based on Linux

Mutual conversion of binary system under linux

Conversion of hexadecimal, decimal, octal and binary under linux

Special symbols of different digits

as follows:

  • Binary: The number prefix is0bor0B, the character set is0and1
  • Octal: the number prefix is0o(zero), character set is0arrive7
  • Decimal: No numeric prefix, character set is0arrive9
  • Hexadecimal: the number prefix is0xor0X, the character set is0arrive9andAarriveF(Either upper and lower case).

Here are some examples:

  • Binary numbers1101Can be expressed as0b1101or0B1101
  • Octal numbers16Can be expressed as020or0o20, where the prefix is0Indicates that it is an octal number.
  • Decimal numbers42No prefix is ​​required.
  • Hexadecimal number1ACan be expressed as0x1Aor0X1a

What should be noted is:

  • When writing a program, the correct prefix should be preceded by the number to clarify the division of the number.
  • Otherwise, the computer might interpret them as digits in different digits.

Doing a binary conversion on a linux shell

To convert each binary to decimal:

Method 1: echo $((now the original value of the binary#))

#2-digit number to decimal numberroot@unassigned:/# echo $((2#1011))
11
#8-digit to decimal numberroot@unassigned:/# echo $((8#13))
11
#Hex to decimal numberroot@unassigned:/# echo $((16#B))
11

Method 2: echo ‘ibase=current binary; original value’ | bc

#8-digit to decimal numberroot@unassigned:~/test# echo 'ibase=8;120' | bc
80
#Hex to decimal numberroot@unassigned:~/test# echo 'ibase=16;50' | bc
80
#2-digit number to decimal numberroot@unassigned:~/test# echo 'ibase=2;1010000' | bc
80

Advanced

Use BC to realize mutual conversion of each cell

echo "obase=target binary; ibase=original; $((original value))" | bc

#Convert decimal value to octalroot@unassigned:~/test# echo "obase=8; ibase=10; $((120))" | bc
170
#Convert decimal to hexadecimalroot@unassigned:~/test# echo "obase=16; ibase=10; $((120))" | bc
78
#Convert decimal to binaryroot@unassigned:~/test# echo "obase=2; ibase=10; $((120))" | bc
1111000

#Octal to binaryroot@unassigned:~/test# echo "obase=2; ibase=8; $((170))" | bc
1111000
#Octal to decimalroot@unassigned:~/test# echo "obase=10; ibase=8; $((170))" | bc
120
#Octal to hexadecimalroot@unassigned:~/test# echo "obase=16; ibase=8; $((170))" | bc
78

#Binary to octalroot@unassigned:~/test# echo "obase=8; ibase=2; $((1111000))" | bc
170
#Convert binary to decimalroot@unassigned:~/test# echo "obase=10; ibase=2; $((1111000))" | bc
120
#Convert binary to hexadecimalroot@unassigned:~/test# echo "obase=16; ibase=2; $((1111000))" | bc
78

#Convert hexadecimal to binaryroot@unassigned:~/test# echo "obase=2; ibase=16; $((78))" | bc
1111000
#Convert hexadecimal to octalroot@unassigned:~/test# echo "obase=8; ibase=16; $((78))" | bc
170
#Convert hexadecimal to decimalroot@unassigned:~/test# echo "obase=10; ibase=16; $((78))" | bc
120

The above briefly introduces the primary conversion under the shell. Let’s talk about the primary conversion in Python:

In python3, there are the following binary conversion methods, corresponding to different binary

hexadecimalhex(), decimalint(), octaloct(), binarybin()

It is worth noting that python is more flexible, and its values ​​need to be distinguished with the corresponding prefix.

for example:

  • Hexadecimal number:0xAA
  • Decimal number:10
  • Octal number:0o20
  • Binary number:0b110

The above four types of calculus have no prefixes, except for the decimal system, the other calculus has their prefixes for identification, such as0x,0o,0b

Conversion effect display:

>>> hex(120)
'0x78'
>>> hex(0o170)
'0x78'
>>> hex(0b1111000)
'0x78'
>>> int(0x78)
120
>>> oct(0x78)
'0o170'
>>> bin(0x78)
'0b1111000'

I won't list them all one by one. The above is the conversion method of the binary system under Linux shell and Python3.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.