comp.lang.ada
 help / color / mirror / Atom feed
From: adambeneschan@gmail.com
Subject: Re: character literals
Date: Tue, 11 Feb 2014 18:03:18 -0800 (PST)
Date: 2014-02-11T18:03:18-08:00	[thread overview]
Message-ID: <96f354e6-789d-4cc6-bb44-e0abafb95e3f@googlegroups.com> (raw)
In-Reply-To: <bhjlf955s7h2f45ptombhodervthlrdji5@4ax.com>

On Tuesday, February 11, 2014 5:34:20 PM UTC-8, ag...@drrob1.com wrote:

> But now I don't know why this function is failing.  I want to convert
> a character value to its integer value.  Modula-2 used the built-in
> function ORD for this purpose.
> 
> This fails type checking
> 
>   I := Natural'Value(CH);

To get the ORD of a character:

    I := Character'Pos (Ch);   
        -- short for "position", i.e. the position of the character literal in        
        -- the list of enumeration literals, which for Character is just the
        -- list of all 256 Extended ASCII characters in order

If Ch is '0', then I will be 48 (the ASCII value of the character '0').

To go the other way:

    Ch := Character'Val (I);

Natural'Value does something completely different.  It takes a string, parses it, and expects the result to be a "Natural".  It raises an exception if the string doesn't have the right format for a "Natural" or is an integer that's out of range.  

In your code, Natural'Value should work fine if Ch is in the range '0' .. '9', because then S will be a validly formatted Natural.  The result will be in the range 0 to 9, not 48 to 57 as Character'Pos (and I think ORD) would give you.  (I don't know Modula-2, but ORD would return 48..57 in Pascal.)  If Ch is something outside that range, then Natural'Value will raise Constraint_Error.  If you think Ch is really in the range '0' .. '9', but you're still getting Constraint_Error on the Natural'Value call, then I'd need to see more of the code.

                                -- Adam

  reply	other threads:[~2014-02-12  2:03 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-11 22:27 character literals agent
2014-02-11 22:49 ` J-P. Rosen
2014-02-11 23:45   ` Bill Findlay
2014-02-11 23:49     ` Ludovic Brenta
2014-02-11 23:58       ` adambeneschan
2014-02-11 23:56 ` adambeneschan
2014-02-12  0:18   ` adambeneschan
2014-02-12  1:34     ` agent
2014-02-12  2:03       ` adambeneschan [this message]
2014-02-12 12:50         ` agent
2014-02-12  1:30   ` Jeffrey Carter
2014-02-12  1:50     ` adambeneschan
2014-02-12 15:53   ` Robert A Duff
2014-02-12 17:55     ` J-P. Rosen
2014-02-14 12:39       ` agent
2014-02-14 18:36         ` AdaControl was: " Simon Clubley
2014-02-15  6:26         ` J-P. Rosen
replies disabled

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