Help - Search - Members - Calendar
Full Version: Echoing the integer-value of a character
MandrivaUsers.org > Advanced Topics > Command Line, Kernel and Programming
satelliteuser083
I'd like to echo the interger-value of single characters entered via stdin; in pascal I'd write something like:
print int(inchar)
but I can't find the bash-script equivalent of 'int'. Can anyone help? Thanks.
tyme
bash doesn't support variable types. in bash, all variable are essentially character strings. as far as how to do what you're looking to do, I'm not really sure...
Steve Scrimpshire
Easy one:


read var1
echo $var1
neddie
That just returns the character here.
What I think he wants is the character code, the ascii value of the character entered. But I don't know how to answer it, sorry.
satelliteuser083
Yes, neddie, that is what I wanted and your suggestion does only return the character, steve. Thanks, nevertheless. 2thumbsup.gif

As it happens, I've managed to solve my problem another way, so knowing the value is unimportant. What is bugging me at the moment is the following:

I want to return one of three possible values from a script back to its caller, using "return result_value" but get the message
CODE
can only `return' from a function or sourced script

Can one of you tell me what "sourced script" means, and, possibly, how to overcome the problem. Thanks
satelliteuser083
I stumbled across the following on http://www.webservertalk.com/message1585326.html ; and it does exactly what I need. : tongue.gif

CODE
ord()
{
if [[ $1 ]]
then
  echo "$(od -td1 <<<"$1" | awk 'NR==1{print $2}')"
else
  exit 1
fi
}

for i
do
  for ((j=0; j < ${#i}; ++j))
  do
    ch=${i:$j:1}
    dec=$(ord "$ch")
    echo -e "char: '$ch'\t ord: $dec"
  done
done


Great stuff. 2thumbsup.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.