Jump to content
satelliteuser083

Echoing the integer-value of a character

Recommended Posts

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.

Edited by satelliteuser083

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Yes, neddie, that is what I wanted and your suggestion does only return the character, steve. Thanks, nevertheless. :thumbs:

 

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

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

Share this post


Link to post
Share on other sites

I stumbled across the following on http://www.webservertalk.com/message1585326.html ; and it does exactly what I need. : :P

 

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. :thumbs:

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...