comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!usen et.coe.montana.edu!milton!mfeldman@ucbvax.Berkeley.EDU  (Michael Feldman)
Subject: Re: Converting integers to char's
Date: 17 Oct 91 04:34:35 GMT	[thread overview]
Message-ID: <1991Oct17.043435.20305@milton.u.washington.edu> (raw)

In article <9426@gara.une.oz.au> cfiddyme@gara.une.oz.au (Kith Kanan) writes:
>
>   Is there any easy way to convert an integer variable to a character.
>   I am new to ada and would like to do this without a case statement.
>                        thanks Chris.
>
I assume that what you're looking for is an Ada equivalent to Pascal's
CHR function, or Basic's CHR$ function, to which you pass a nonnegative
integer (presumably in the range 0..127) and get back the corresponding
ASCII character.

Yes, there's an easy way. It's called the Val attribute. Given some
integer X and a character variable Ch,
   Ch := Character'Val(X);
will do the trick, raising Constraint_Error if X's value is out of the
ASCII range.

By the way, going the other way is equally easy.
   X := Character'Pos(Ch);
stores in X the position of Ch in the type Character, which in effect
gets you the ASCII value.

Pos, Val, and lots of other attributes are defined uniformly for
discrete types (integers, enumerations).

Attributes are neat in Ada (though I think they could be neater if they
were more complete and uniform). Check your Ada book or the LRM on this.

Mike

-------------------------------------------------------------------------------
Michael B. Feldman
Visiting Professor 1991-92               Professor
Dept. of Comp. Sci. and Engrg.           Dept. of Elect. Engrg. and Comp. Sci.
University of Washington FR-35           The George Washington University
Seattle, WA 98105                        Washington, DC 20052

mfeldman@cs.washington.edu               mfeldman@seas.gwu.edu
(206) 632-3794 (voice)                   (202) 994-5253 (voice)
(206) 543-2969 (fax)                     (202) 994-5296 (fax)
-------------------------------------------------------------------------------

             reply	other threads:[~1991-10-17  4:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-10-17  4:34 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!usen [this message]
  -- strict thread matches above, loose matches on Subject: below --
1991-10-16  1:33 Converting integers to char's agate!spool.mu.edu!sol.ctr.columbia.edu!samsung!munnari.oz.au!mel.dit.csi
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox