Differences between revisions 6 and 7
Revision 6 as of 2008-11-22 23:11:21
Size: 1733
Editor: GreyCat
Comment: first-line
Revision 7 as of 2009-10-12 18:50:53
Size: 1840
Comment: added function to convert a number in a given base to decimal
Deletions are marked like this. Additions are marked like this.
Line 36: Line 36:
   todec() {
      echo $(( $1#$2 ))
   }
Line 40: Line 44:

   todec 16 ffe # -> 4094
   todec 2 100100 # -> 36

How do I convert an ASCII character to its decimal (or hexadecimal) value and back?

If you have a known octal or hexadecimal value (at script-writing time), you can just use printf:

   # POSIX
   printf '\x27\047\n'

This prints two literal ' characters (27 is the hexadecimal ASCII value of the character, and 47 is the octal value) and a newline.

If you need to convert characters (or numeric ASCII values) that are not known in advance (i.e., in variables), you can use something a little more complicated:

   # POSIX
   # chr() - converts decimal value to its ASCII character representation
   # ord() - converts ASCII character to its decimal value
 
   chr() {
     printf \\$(printf '%03o' $1)
   }
 
   ord() {
     printf '%d' "'$1"
   }

   # hex() - converts ASCII character to a hexadecimal value
   # unhex() - converts a hexadecimal value to an ASCII character

   hex() { 
      printf '%x' "'$1"
   }

   unhex() {
      printf \\x"$1"
   }

   todec() {
      echo $(( $1#$2 ))
   }

   # examples:
 
   chr $(ord A)    # -> A
   ord $(chr 65)   # -> 65

   todec 16 ffe    # -> 4094
   todec 2 100100  # -> 36

The ord function above is quite tricky.

  • Tricky? Rather, it's using a feature that I can't find documented anywhere -- putting a single quote in front of an integer. Neat effect, but how on earth did you find out about it? Source diving? -- GreyCat

    • It validates The Single Unix Specification: "If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote." (see printf() to know more) -- mjf

BashFAQ/071 (last edited 2021-02-08 16:03:51 by GreyCat)