Functions for ord and chr in bash shell scripting

In python there are couple of useful function. these are ord function and chr function.

ord function in python - it take character as an argument and return its ASCII value.

chr function - it takes integer values as an argument and returns character that is equivalent to the integer value.

I am going to introduce function to do same thing in bash shell scripting. I named it as ord.

ord() {

  LC_CTYPE=C printf '%d' "'$1"
  return $LC_CTYPE

}



My next function is chr function. it takes integer value as an argument and returns correspponding ASCII value.

chr() {
  [ "$1" -lt 256 ] || return 1
  printf "\\$(printf '%03o' "$1")"
}

0 comments:

Post a Comment

Ask anything about this Tutorial.