Web Server forum
Back To The Forum Home!Search!Private Messaging System

This is Interesting: Free IT Magazines Now Free shipping to California  
Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > ASCII value of character




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    ASCII value of character  
Dave Farrance


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-16-06 06:20 PM

The "Advanced Bash-Scripting Guide" has a section on string manipulation
that's useful. I can find the equivalent of most of the BASIC string
functions that I used a quarter of a century ago.

I've now figured out (with a little help from this group) that the Bash
equivalent of the BASIC CHR$() function for converting a decimal number
into its ASCII character is:

$(printf "\\$(printf "%03o" $x)")

Can Bash go the other way - from a character to its ASCII decimal number
- like the BASIC ASC() function?

--
Dave Farrance





[ Post a follow-up to this message ]



    Re: ASCII value of character  
Steffen Schuler


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-16-06 06:20 PM

Dave Farrance wrote:
> The "Advanced Bash-Scripting Guide" has a section on string manipulation
> that's useful. I can find the equivalent of most of the BASIC string
> functions that I used a quarter of a century ago.
>
> I've now figured out (with a little help from this group) that the Bash
> equivalent of the BASIC CHR$() function for converting a decimal number
> into its ASCII character is:
>
>    $(printf "\\$(printf "%03o" $x)")
>
> Can Bash go the other way - from a character to its ASCII decimal number
> - like the BASIC ASC() function?
>

A solution with bash:

-------------------------begin-----------------------------
#!/bin/bash

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


better use perl:

ch=0; PERL -e "print ord($ch),  \"\n\";"


Regards

Steffen Schuler





[ Post a follow-up to this message ]



    Re: ASCII value of character  
Steffen Schuler


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-16-06 06:20 PM

Steffen Schuler wrote:
> Dave Farrance wrote:
> 
>
>
> A solution with bash:
>
> -------------------------begin-----------------------------
> #!/bin/bash
>
> 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
> ------------------------end--------------------------------
>
>
> better use perl:
>
> ch=0; PERL -e "print ord($ch),  \"\n\";"
>
>
> Regards
>
> Steffen Schuler
the echo in the bash ord() function is superfluous.

Regards

Steffen Schuler





[ Post a follow-up to this message ]



    Re: ASCII value of character  
Dave Farrance


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-16-06 06:20 PM

Steffen Schuler <schuler.steffenDELETETHIS@gmx.de> wrote:

>Dave Farrance wrote: 
[vbcol=seagreen]
> ...
> echo "$(od -td1 <<<"$1" | awk 'NR==1{print $2}')"
> ...
> ch=0; PERL -e "print ord($ch),  \"\n\";"

Thanks. Looks like there's no way with bash builtins.

BTW, quotes are needed in that PERL function when called from bash.

$ ch=m; PERL -e "print ord($ch),  \"\n\";"
Search pattern not terminated at -e line 1.
$ ch=m; PERL -e "print ord('$ch'),  \"\n\";"
109
_
--
Dave Farrance





[ Post a follow-up to this message ]



    Re: ASCII value of character  
Steffen Schuler


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-16-06 06:20 PM

Dave Farrance wrote:
> Steffen Schuler <schuler.steffenDELETETHIS@gmx.de> wrote:
>
> 
>
> 
>
>
> Thanks. Looks like there's no way with bash builtins.
>
> BTW, quotes are needed in that PERL function when called from bash.
>
> $ ch=m; PERL -e "print ord($ch),  \"\n\";"
> Search pattern not terminated at -e line 1.
> $ ch=m; PERL -e "print ord('$ch'),  \"\n\";"
> 109
>

A solution with only bash builtins with demo:


#!/bin/bash

char()
{
printf \\$(printf "%03o" "$1")
}

asc()
{
local i
for ((i=1; i<256; ++i))
do
if [ "$(char "$i")" = "$1" ]
then
echo "$i"
break
fi
done
return 1
}

for ((i=32; i<127; ++i))
do
ch=$(char "$i")
printf "ASC('%s') = % 3d\n" "$ch" "$(asc "$ch")"
done

Regards

Steffen Schuler





[ Post a follow-up to this message ]



    Re: ASCII value of character  
Steffen Schuler


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-17-06 12:20 AM

Steffen Schuler wrote:
> Steffen Schuler wrote:
> 
>
>
> replace the 'break' in asc() by 'return 0'

A significantly faster pure bash script based on binary search:

---------------------------begin--------------------------
#!/bin/bash

char()
{
printf \\$(printf "%03o" "$1")
}

asc()
{
local ch="$1"
local low hi mid ch_mid
low=1
hi=255
while ((low+1<hi))
do
((mid=(low+hi)/2))
ch_mid=$(char "$mid")
if [[ "$ch_mid" < "$ch" ]]
then
low="$mid"
elif [[ "$ch_mid" > "$ch" ]]
then
hi="$mid"
else
echo "$mid"
return 0
fi
done
return 1
}

for ((i=32; i<127; ++i))
do
ch=$(char "$i")
printf "ASC('%s') = %3d\n" "$ch" "$(asc "$ch")"
done
------------------------end------------------------------

Regards

Steffen Schuler





[ Post a follow-up to this message ]



    Re: ASCII value of character  
John W. Krahn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-17-06 12:20 AM

Dave Farrance wrote:
> Steffen Schuler <schuler.steffenDELETETHIS@gmx.de> wrote:
> 
> 
>
> Thanks. Looks like there's no way with bash builtins.
>
> BTW, quotes are needed in that PERL function when called from bash.
>
> $ ch=m; PERL -e "print ord($ch),  \"\n\";"
> Search pattern not terminated at -e line 1.
> $ ch=m; PERL -e "print ord('$ch'),  \"\n\";"
> 109

Or more simply:

$ ch=m; PERL -le "print ord'$ch'"
109

And the other way:

$ ch=109; PERL -le "print chr'$ch'"
m



John
--
use Perl;
program
fulfillment





[ Post a follow-up to this message ]



    Re: ASCII value of character  
Stachu 'Dozzie' K.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-17-06 12:20 AM

On 16.07.2006, John W. Krahn <someone@example.com> wrote:
> Dave Farrance wrote: 
>
> Or more simply:
>
> $ ch=m; PERL -le "print ord'$ch'"
> 109
>
> And the other way:
>
> $ ch=109; PERL -le "print chr'$ch'"
> m

OMG! What the hell are you doing to Perl? It hurts!

#v+
ch=m PERL -le 'print ord $ENV{ch}'
perl -le 'print ord shift' m
#v-

--
Stanislaw Klekot





[ Post a follow-up to this message ]



    Re: ASCII value of character  
William Park


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-17-06 12:20 AM

Dave Farrance <DaveFarrance@omitthisyahooandthis.co.uk> wrote:
> The "Advanced Bash-Scripting Guide" has a section on string manipulation
> that's useful. I can find the equivalent of most of the BASIC string
> functions that I used a quarter of a century ago.
>
> I've now figured out (with a little help from this group) that the Bash
> equivalent of the BASIC CHR$() function for converting a decimal number
> into its ASCII character is:
>
>    $(printf "\\$(printf "%03o" $x)")
>
> Can Bash go the other way - from a character to its ASCII decimal number
> - like the BASIC ASC() function?

http://home.eol.ca/~parkw/index.html#ctype.h

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/





[ Post a follow-up to this message ]



    Re: ASCII value of character  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-17-06 12:20 AM

On 2006-07-16, Dave Farrance wrote:
> The "Advanced Bash-Scripting Guide" has a section on string manipulation
> that's useful. I can find the equivalent of most of the BASIC string
> functions that I used a quarter of a century ago.
>
> I've now figured out (with a little help from this group) that the Bash
> equivalent of the BASIC CHR$() function for converting a decimal number
> into its ASCII character is:
>
>    $(printf "\\$(printf "%03o" $x)")
>
> Can Bash go the other way - from a character to its ASCII decimal number
> - like the BASIC ASC() function?

From the SUS printf  spec:

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

E.g.:

CHAR=A
printf "%d\n" "'$CHAR"


--
Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:20 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register