Shell character to ascii code or number Conversion

Source: Internet
Author: User

One idea is to use awk to first create a conversion table between characters and ascii codes or numbers in the awin of awk, and then read the character table to be converted to output the corresponding conversion code. The following sample code converts a letter A-Z to a number 1-26, because shell is a string at the language level, so this conversion is called a better ing of one character to another.

 

Digit = 'echo $ {letter} | awk-v letter_cnt = 26' <br/> BEGIN {<br/> I = 0; <br/> for (I = 0; I <letter_cnt; I ++) <br/>{< br/> ch = sprintf ("% c", I + 65 ); <br/> trans_table [ch] = I; <br/>}< br/>{< br/> if (NF = 1) <br/>{< br/> printf ("% d", trans_table [$1]); <br/>}< br/>} ''<br/> if [$? -Ne 0-o "x $ {digit}" = "x"]; then <br/> echo "invalid letter, must be [A-Z] "<br/> else <br/> echo" transform right: $ {letter} -- >$ {digit} "<br/> fi <br/>

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.