Shell-in-the-system conversion

Source: Internet
Author: User
Tags ibase

Using Shell operations for a binary conversion

Assuming there are some numbers in your script, you need to process those numbers in a different binary. This type of conversion can be implemented easily and automatically using Shell operations. One scenario is to use a Shell operation to convert a number from a given binary bit to a decimal. If the number is provided as an operation expansion, it is assumed to have a decimal notation unless it precedes with 0 (which is assumed to be octal) or 0x (this is assumed to be hexadecimal). Type the following to get decimal output for some octal and hexadecimal values:

Echo $ ((013)) echo $ ((0xa4))

You can also specify any binary from 2 to 64 using the following format:

$ ((base#number))

Try converting binary, octal, 16, and other binary numbers to decimal by typing the line shown in Listing 7 at the shell prompt.


Listing 7. The number of arbitrary binaries in the Shell is output in decimal

echo $ ((2#1101010)) echo $ ((8#377)) echo $ ((16#D8)) echo $ ((12#10)) echo $ ((36#ZZYY))

Using bc a binary conversion

Another trick to making a binary conversion in the Shell is to use bc it, which is an arbitrary precision computing language, which is provided by most UNIX installers. Because it allows you to specify the output input, this is a good technique when you need to export to a binary output other than decimal.

bcibaseand obase contains the values for the input and output respectively. By default, it is set to 10. To perform a binary conversion, you need to change one or two of these values, and then provide a number. Try it now, as shown in Listing 8.


Listing 8. Perform a binary conversion using BC

BC-QL Tenobase=16Aibase=22control-d$

To quickly perform a binary conversion, you can combine bc and echo form a quick single command-line program to transfer a given value through a pipeline bc . Type the content shown in Listing 9.


Listing 9. Shell single command line BC program

echo ' obase=16; | BC Echo ' obase=10; ibase=16; A03 ' | BC2563$

Warning: When you set bc the input into, bc all the numbers you enter are used in the binary, including the numbers you provide to set the output. Therefore, it is better to set the output input first, otherwise it may produce unexpected results, as shown in Listing 10.


Listing 10. Importance of setting the sequencing of input and output inputs

Echo ' ibase=16; obase=10; A ' | BCEcho ' ibase=16; obase=a; A ' | BC10$

Shell-in-the-system conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.