Converting numbers on Linux among decimal, hexadecimal, octal, and binary

Linuxprovides commands for converting numbers from one base to another. Learn how to uses these commands and how to make the process easier with scripts and aliases.

  • 在脸书上分享
  • Share on Twitter
  • Share on LinkedIn
  • 分享Reddit
  • 通过电子邮件分享
  • Print resource
Encryption  >  Encrypted data / hexadecimal code
Matejmo / Getty Images

You might not be challenged very often to convert numbers from one numbering system to another but, when you are, you can do it with either of two fairly easy commands on the Linux command line.

Converting in your head can be taxing, especially for longer numbers. While the decimal numbering system allows any digit in a number to have any of ten values (0-9), digits in hexadecimal numbers can have 16 (0-F), digits in octal numbers only eight (0-7) and digits in binary numbers only two (0-1).

And, whether you like it or not, from time to time you are likely to run into numbers displayed in hex or octal, and knowing how to convert them from one number base to another can come in handy.

To get started, the decimal numbers 5 to 16 in the four numbering systems look like this:

5 6 7 8 9 10 11 12 13 14 15 16 <== decimal
5 6 7 8 9 a b c d e f 10 <== hexadecimal 5 6 7 10 11 12 13 14 15 16 17 20 <== octal 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 <== binary

Using theprintfcommand to convert decimal numbers

要将数字从十进制转换为十六进制,您可以使用printfcommand like the one below that converts decimal 15 to hexadecimal:

$ printf ‘%x\n’ 15 f

要转换为八进制,请替换“%X”(十六进制)用“%o”(八分之一):

$ printf ‘%o\n’ 15 17

对于上面的第二个示例中所示的17个,请回想一下,第一个数字(1)的小数为8,第二个数字为8,因此值为7。因此,八分音结果等于十进制15。

Turning these conversions into scripts is easy. Here are two scripts that will prompt for the number to be converted and then run theprintfcommand to do the conversion for you.

dec2hex

#!/bin/bash echo -n“输入十进制数字>”读取号码printf'%x \ n $ number

dec2oct

#!/bin/bash echo -n “Enter a decimal number> “ read number printf ‘%o\n’ $number

注意:printfcommand doesn’t have an option for converting to binary.

Using the公元前command to convert decimal numbers

Usingthe公元前(calculator) command, you can do the same type of conversions with commands like those shown below.

The first example converts the decimal number 16 to base 16 (hexadecimal). Since in hexadecimal the second digit from the right represents the 16s position (that’s the decimal 16), the 1 means decimal 16. The “obase” setting means “output base”. This is the format you want the number to be converted to. Because the “ibase” (input base) is not provided, it is assumed to be decimal.

$ echo “obase=16; 16” | bc 10

下一个命令将相同的数字转换为八进制。由于在八进制中,右边的第二个数字表示十进制8S位置,因此2表示十进制16(两个8s)。

$ echo“ obase = 8;16英寸|BC 20

下一个更大的数字,十进制17,我们得到21-十进制的8s和1个结果。

$ echo“ obase = 8;17” | bc 21

The公元前命令与二进制文件一样,与十六进制和八进制一样,因此我们也可以将数字转换为二进制。这是将十进制16显示为二进制编号的命令:

$ echo “obase=2; 16” | bc 10000

Described in base 10, that’s one 16, no 8s, no 4s, no 2s, and no 1s.

尽管二进制系统之所以使用,因为它直接与电子状态合作,但八分和十六进制系统提供了简单的转换,因此我们人类不必查看0和1的长字符串。即使这也不限制公元前command which has no problem working with a base 3, base 7 or any other numbering base that you might want to experiment with.

$ echo“ obase = 3;16英寸|BC 121

被描述为十进制数字,上面示例中的121表示一个9、2 3和一个1。这是基本7版本:

$ echo “obase=7; 16” | bc 22

The digits above represent two decimal 7’s and two 1’s.

Converting numbers to decimal with公元前

With all that said, you can also use the公元前命令将在十六进制,八元或二进制中表达为十进制的数字。只是指定obase(output base) value as 10, theibase(输入基库)数字为16,如果十六进制的起始数,则为8八分,如果是二进制,则如下所示。

$ echo “obase=10; ibase=16; F” | bc 15 $ echo “obase=10; ibase=8; 15” | bc 13 $ echo “obase=10; ibase=2; 112” | bc 7

就像在较早的情况下一样,将这些命令变成脚本只需要一点努力。

hex2dec

#!/bin/bash echo -n“ type a ax number>”读取号码echo“ obase = 10;IBase = 16;$数字” |公元前

oct2dec

#!/bin/bash echo -n “Type a octal number> “ read number echo “obase=10; ibase=8; $number” | bc

bin2dec

#!/bin/bash echo -n “Type a binary number> “ read number echo “obase=10; ibase=16; $number” | bc

使用别名进行数字基础转换

While the scripts shown above are easy to set up, probably the most convenient way to implement any of the conversions shown is to set them up as a group of aliases. Adding the alias below to your〜/.bashrcfile would make them available on your next login or as soon as you source the file (e.g., by running。〜/.bashrc).

#从十进制别名dec2hex =’f(){echo“ obase = 16;IBase = 10;$ 1” |公元前;unset -f f;};f’alias dec2oct =’f(){echo“ obase = 8;IBase = 10;$ 1” |公元前; unset -f f; }; f’ alias dec2bin=’f(){ echo “obase=2; ibase=10; $1” | bc; unset -f f; }; f’ # convert to decimal alias hex2dec=’f(){ echo “obase=10; ibase=16; $1” | bc; unset -f f; }; f’ alias oct2dec=’f(){ echo “obase=10; ibase=8; $1” | bc; unset -f f; }; f’ alias bin2dec=’f(){ echo “obase=10; ibase=2; $1” | bc; unset -f f; }; f’

Afterwards, you can run the aliases like this:

$ dec2hex 15 F $ dec2oct 15 17 $ dec2bin 15 1111 $ hex2dec F 15 $ oct2dec 17 15 $ bin2dec 1111 15

包起来

Converting numbers from one numbering system to another can be tedious, but you can save yourself a lot of trouble using theprintf公元前commands, especially if you set them up as scripts or aliases.

Related:

版权所有©2022 IDG Com足球竞彩网下载munications,Inc。

The 10 most powerful companies in enterprise networking 2022