# Use Shell for hexadecimal conversion

Source: Internet
Author: User
Tags ibase

Using Shell for hexadecimal conversion assumes that there are some numbers in your script, and you need to process them in another hexadecimal format. Shell operations can easily automatically implement such conversions. One case is to use Shell to convert a number from the given hexadecimal format to the decimal format. If a number is provided in the form of an arithmetic expansion, it is assumed to have a decimal sign unless it is preceded by 0 (in this case it is assumed to be octal) or 0x (in this case it is assumed to be hexadecimal ). Type the following content to get the decimal output of some octal and hexadecimal values: \$ echo \$ (013) \$ echo \$(0xA4 )) you can also specify any hexadecimal format between 2 and 64 using the following format: \$ (BASE # NUMBER) by typing the rows shown in listing 7 at the Shell prompt, convert binary, octal, hexadecimal, and other hexadecimal numbers to decimal. Listing 7. output echo \$(2 #1101010) echo \$(8 #377) echo \$(16 # D8) in decimal places in Shell )) echo \$(12 #10) echo \$(36 # ZZYY) another way to use bc for hexadecimal conversion in Shell is to use bc, it is an arbitrary precision computing language provided by most UNIX/Linux installers. Because it allows you to specify the output in hexadecimal format, this is a good technique when you need to output in decimal format. The special variables ibase and obase of bc respectively contain the hexadecimal values used for input and output. By default, all are set to 10. To perform the hexadecimal conversion, you need to change one or two values and then provide a number. Try now, as shown in listing 8. Listing 8. use bc to execute the hexadecimal conversion \$ bc-ql 1010 obase = 16 10 Aibase = 2 102 to quickly execute the hexadecimal conversion, you can use bc and echo together to form a fast single command line program, transmits the given value to bc in a pipeline. Type the content shown in listing 9. Listing 9. shell single command line bc program \$ echo 'obase = 16; 47 '| bc2F \$ echo 'obase = 10; ibase = 16; a03' | bc2563 warning: after you set the bc input in hexadecimal format, all numbers in the input bc use this hexadecimal format, including the numbers you provide for setting the output in hexadecimal format. Therefore, it is best to set the output hexadecimal format first, otherwise unexpected results may occur, as shown in listing 10. Listing 10. importance of setting Input and Output hexadecimal order \$ echo 'ibase = 16; obase = 10; a' | bcA \$ echo 'ibase = 16; obase =; a' | bc10

Related Keywords:

### 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.