Differences between revisions 15 and 16
Revision 15 as of 2009-10-16 14:55:19
Size: 2668
Comment: My octal conversion formula was way wrong. Sorry.
Revision 16 as of 2009-10-17 07:22:24
Size: 1903
Editor: ppp089210051033
Comment: remove all the blah, add the faster version with maths in the text
Deletions are marked like this. Additions are marked like this.
Line 22: Line 22:
      #Another version doing the octal conversion with arithmetic
   # faster as it avoids a subshell
  chr () {
    printf \\$(($1/64*100+$1%64/8*10+$1%8))
   }
Line 47: Line 53:


Some versions avoiding a subshell:
{{{
oldchr () { printf \\$(printf '%03o' $1) ;}

#posix
chr () {
    set -- $(($1 / 64)) $(($1 % 64))
    set -- $1 $(($2 / 8)) $(($2 % 8))
    printf \\"${1}${2}${3}"
}

#bash only
chr_bash () {
    local temp
    printf -v temp '%03o' $1
    printf \\$temp
}

#test
for i in {1..255} ;do [[ "$(oldchr $i)" = "$(chr $i)" ]] || echo $i;done
for i in {1..255} ;do [[ "$(oldchr $i)" = "$(chr_bash $i)" ]] || echo $i;done
for p in oldchr chr chr_bash; do echo $p:;time for i in {1..4000}; do $p 65 >/dev/null; done; done

}}}
the timings:
{{{
$ bash chr
oldchr:

real 0m14.350s
user 0m5.004s
sys 0m9.248s
chr:

real 0m0.422s
user 0m0.059s
sys 0m0.216s
chr_bash:

real 0m0.400s
user 0m0.042s
sys 0m0.189s

}}}

Yet another version probably faster:
{{{
chr () {
    printf \\$(($1/64*100+$1%64/8*10+$1%8))
}


}}}

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)
   }
  
   #Another version doing the octal conversion with arithmetic 
   # faster as it avoids a subshell
  chr () {
    printf \\$(($1/64*100+$1%64/8*10+$1%8))
   } 

   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

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