Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get ascii value of a character?

1,111 views
Skip to first unread message

Timster

unread,
May 15, 1996, 3:00:00 AM5/15/96
to

Hi, I am rather new to Tcl. I do have the Ousterhout book, but I
can't seem to find the the command that gives me the ASCII value of a
character. I want something like:

set asciiValue [ascii [string index myString 4]]

E-mail to me at tim...@metronet.com. Thanks.

-T

Jeffrey Hobbs

unread,
May 15, 1996, 3:00:00 AM5/15/96
to

In article <4nbvcd$f...@fohnix.metronet.com>,

Timster <tim...@fohnix.metronet.com> wrote:
> the command that gives me the ASCII value of a character.

scan $char %c asciiValue
--
Jeffrey Hobbs Office: 541/346-3998
Univ of Oregon CIS GRF email: jho...@cs.uoregon.edu
URL: http://www.cs.uoregon.edu/~jhobbs/

Marc Thirion

unread,
May 15, 1996, 3:00:00 AM5/15/96
to Timster

In article <4nbvcd$f...@fohnix.metronet.com>,

tim...@fohnix.metronet.com (Timster) writes:
>
>Hi, I am rather new to Tcl. I do have the Ousterhout book, but I
>can't seem to find the the command that gives me the ASCII value of a
>character. I want something like:
>
> set asciiValue [ascii [string index myString 4]]

This one is a bit tricky.
Here is one solution (no doubt there are better ones).
First, set up an array : char -> ascii (at the global level)
for {set i 1} {$i < 128} {incr i} {
set arr([format %c $i]) $i
}
and then define ascii :
proc ascii {c} {
uplevel #0 set arr($c)
}
[tested with tcl7.4]

You may also want to go beyond ASCII and cope also with
characters > 7 bits (we are billions out there whose
language need more than 7 bits values :-)

>E-mail to me at tim...@metronet.com. Thanks.

posted and emailed.

--
Marc Thirion -- Linux -- | 150 Bd Deodat de Severac
work: thi...@verilog.fr (preferred) | 31300 Toulouse, France
home: 10074...@compuserve.com | (33) 61 42 01 30


Gerald W. Lester

unread,
May 17, 1996, 3:00:00 AM5/17/96
to Timster

Timster wrote:
>
> Hi, I am rather new to Tcl. I do have the Ousterhout book, but I
> can't seem to find the the command that gives me the ASCII value of a
> character. I want something like:
>
> set asciiValue [ascii [string index myString 4]]

Try:

scan [string index myString 4] %c asciiValue

Although you might really want:

set myString testme
scan [string index $myString 4] %c asciiValue

**********************************************************************
* Gerald W. Lester | Voice: +1 (504)-889-2784 *
* Computerized Processes Unlimited, Inc. | FAX: +1 (504)-889-2799 *
* 4200 S. I-10 Service Rd., Suite 205 | E-Mail: g...@cpu.com *
* Metairie, LA 70001 | WWW: http://www.cpu.com *
* "Consulting System Integrators" *
**********************************************************************

Larry W. Virden

unread,
May 17, 1996, 3:00:00 AM5/17/96
to

Why not just do:

tcl>set myString "This is a test"
tcl>scan [string index myString 4] "%c" asciiValue
1
tcl>puts $asciiValue
114


Of course, you perhaps don't want the decimal value of the character -
you would have to convert that number then.
--
:s Larry W. Virden INET: lvi...@cas.org
:s <URL:http://www.teraform.com/%7Elvirden/> <*> O- "We are all Kosh."
:s Unless explicitly stated to the contrary, nothing in this posting should
:s be construed as representing my employer's opinions.

0 new messages