Differences between revisions 10 and 12 (spanning 2 versions)
Revision 10 as of 2009-10-16 07:47:19
Size: 1931
Editor: pgas
Comment: move the faster solution with the other examples, using printf for more portability
Revision 12 as of 2009-10-16 08:07:34
Size: 2617
Editor: pgas
Comment: note about limitations
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
Line 12: Line 13:
Line 16: Line 18:
 
Line 19: Line 21:
   }
 
   # another faster version
   # the octal conversion is done using maths, potentially avoiding a subshell and a fork
   chr() {
     printf "\0$(( $1 % 8 + 10 * ( $1 / 8 ) + 20 ))";
Line 34: Line 30:
   hex() {     hex() {
Line 43: Line 39:
 
Line 47: Line 43:
Line 50: Line 45:
 ''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''  . ''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 [[http://www.opengroup.org/onlinepubs/009695399/utilities/printf.html|printf()]] to know more) -- mjf''
Line 52: Line 48:
  ''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 [[http://www.opengroup.org/onlinepubs/009695399/utilities/printf.html|printf()]] to know more) -- mjf'' This version of {{{chr}}} executes much faster than the {{{printf}}} version above (about 1/40 to less than 1/150 the time when run in a loop):

{{{
   chr() { echo -en "\0$(( $1 % 8 + 10 * ( $1 / 8 ) + 20 ))"; }
}}}
{{{
   for p in chr newchr; do time for i in {1..4000}; do $p 65 >/dev/null; done; done

   System1 System2
   real 0m46.824s real 1m33.814s
   user 0m4.624s user 0m8.540s
   sys 0m33.290s sys 1m23.978s

   real 0m1.340s real 0m0.512s
   user 0m1.096s user 0m0.389s
   sys 0m0.124s sys 0m0.096s
}}}
  This version is faster as it executes without a subshell, it seems to only work strictly on ascii chars <127 while the printf version is happy with chars up to 255 and also for only a subset of ascii ie >64 decimal.


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"
   }

   # examples:

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

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

This version of chr executes much faster than the printf version above (about 1/40 to less than 1/150 the time when run in a loop):

   chr() { echo -en "\0$(( $1 % 8 + 10 * ( $1 / 8 ) + 20 ))"; }

   for p in chr newchr; do time for i in {1..4000}; do $p 65 >/dev/null; done; done

   System1                     System2
   real    0m46.824s           real    1m33.814s
   user    0m4.624s            user    0m8.540s
   sys     0m33.290s           sys     1m23.978s

   real    0m1.340s            real    0m0.512s
   user    0m1.096s            user    0m0.389s
   sys     0m0.124s            sys     0m0.096s
  • This version is faster as it executes without a subshell, it seems to only work strictly on ascii chars <127 while the printf version is happy with chars up to 255 and also for only a subset of ascii ie >64 decimal.

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