comp.lang.ada
 help / color / mirror / Atom feed
* Character/Integer Conversion
@ 2000-03-31  0:00 ebresie
  2000-04-02  0:00 ` DuckE
  2000-04-03  0:00 ` David
  0 siblings, 2 replies; 9+ messages in thread
From: ebresie @ 2000-03-31  0:00 UTC (permalink / raw)


I am sure this is a simple thing, but I am doing the following...

I need to create a string of character which will be sent to a scsi
interface.

I need to convert from a character into a integer so that I can make a
checksum.

I am giving a basic code version...not verbatum

msg : string(1..length);

msg(1) := '~';  -- how do I get the asci value for this character?
msg(2)  := 33;
msg(3)   : msg(1)'value + msg(2)'value;

this should more or less add the ascii value of the first character with
the value of the second.

Any help would be appreciated.


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-03-31  0:00 Character/Integer Conversion ebresie
@ 2000-04-02  0:00 ` DuckE
  2000-04-03  0:00 ` David
  1 sibling, 0 replies; 9+ messages in thread
From: DuckE @ 2000-04-02  0:00 UTC (permalink / raw)


My answer is an example:

function CkSum( msg : String ) return integer is
  result : integer := 0;
begin
  for ii in msg'range loop
    result := result + character'pos( msg( ii ) );
  end loop;
end CkSum;

I hope this helps,

SteveD

<ebresie@usa.net> wrote in message news:8c3a6e$q5p$1@nnrp1.deja.com...
> I am sure this is a simple thing, but I am doing the following...
>
> I need to create a string of character which will be sent to a scsi
> interface.
>
> I need to convert from a character into a integer so that I can make a
> checksum.
>
> I am giving a basic code version...not verbatum
>
> msg : string(1..length);
>
> msg(1) := '~';  -- how do I get the asci value for this character?
> msg(2)  := 33;
> msg(3)   : msg(1)'value + msg(2)'value;
>
> this should more or less add the ascii value of the first character with
> the value of the second.
>
> Any help would be appreciated.
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.






^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-04-03  0:00 ` David
  2000-04-03  0:00   ` Robert Dewar
@ 2000-04-03  0:00   ` Robert Dewar
  2000-04-04  0:00     ` Mats Weber
  1 sibling, 1 reply; 9+ messages in thread
From: Robert Dewar @ 2000-04-03  0:00 UTC (permalink / raw)


In article <8ca7k8$v2q$1@nnrp1.deja.com>,
  David C. Hoos, Sr. <david.c.hoos.sr@ada95.com> wrote:

> I do this all the time, in the following way:
> Msg : String (1 .. Length);
> type Msg_Bytes is array (Msg'Range)of
>       Interfaces.C.Unsigned_Char;
> use type Interfaces.C.Unsigned_Char;
> Msg_Overlay : Msg_Bytes;
> for Msg_Overlay'Address use Msg'Address;

That's a particularly nasty low level hacking solution. It is
inherently non-portable (the whole idea of the existence of
the C.Unsigned_Char type is that it might have a different
representation from Standard.Character), and it is a mistake
to use low level address overlay stuff when there is a perfectly
good solution.

Note also that the effect of address overlays itself is
implementation dependent, there is no requirement on a compiler
that this "work" right, since "working" is not defined in the
RM. In practice it probably wil work, though I would always
make such variables aliased if you absolutely have to use
address overlays.

In addition, it is usually better to use Unchecked_Conversion
than address overlays, since it is better defined, and the use
is flagged in the context clause. Address overlays are
definitely overused. Perhaps it was a mistake in Ada 95
to legitimize these (in Ada 83 they are erroneous, in Ada 95
implementation dependent).

Anyway, the advice in the other message (using the 'Pos
attribute) is a far better approach to your problem.

One more trouble with the above declarations is that there
is no guarantee that the address of an array is necessarily
the address of the first element. The above declarations
may simply result in overlaying dope vectors with peculiar
results!


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-04-03  0:00 ` David
@ 2000-04-03  0:00   ` Robert Dewar
  2000-04-04  0:00     ` Robert A Duff
  2000-04-03  0:00   ` Robert Dewar
  1 sibling, 1 reply; 9+ messages in thread
From: Robert Dewar @ 2000-04-03  0:00 UTC (permalink / raw)


In article <8ca7k8$v2q$1@nnrp1.deja.com>,
  David C. Hoos, Sr. <david.c.hoos.sr@ada95.com> wrote:

> I do this all the time, in the following way:
> Msg : String (1 .. Length);
> type Msg_Bytes is array (Msg'Range)of
>       Interfaces.C.Unsigned_Char;
> use type Interfaces.C.Unsigned_Char;
> Msg_Overlay : Msg_Bytes;
> for Msg_Overlay'Address use Msg'Address;

One more point about the above dubious code.

There is no guarantee that the declaration of Msg_Overlay
will not clobber the valeu in Msg. This probably will not
affect anything, but in other cases it can. It is probably
a good idea to always use a pragma Import (Ada, ..) for
the variable doing the overlaying to inhibit any default
initialization associated with the variable.

For example, in

   x : integer := f (x);
   y : access_to_integer;
   for y'address use x'address;

the declaration of y will likely clobber the initial value
of x, and we should prevent that by either writing:

   x : integer := f (x);
   y : access_to_integer;
   for y'address use x'address;
   pragma Import (Ada, y);

or better by not using address overlays (an unchecked conversion
would be far safer and clearer in this situation).





Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-03-31  0:00 Character/Integer Conversion ebresie
  2000-04-02  0:00 ` DuckE
@ 2000-04-03  0:00 ` David
  2000-04-03  0:00   ` Robert Dewar
  2000-04-03  0:00   ` Robert Dewar
  1 sibling, 2 replies; 9+ messages in thread
From: David @ 2000-04-03  0:00 UTC (permalink / raw)
  To: ebresie

In article <8c3a6e$q5p$1@nnrp1.deja.com>,
ebresie@usa.net wrote:
> I am sure this is a simple thing, but I am doing the following...
>
> I need to create a string of character which will be sent to a scsi
> interface.
>
> I need to convert from a character into a integer so that I can make a
> checksum.
>
> I am giving a basic code version...not verbatum
>
> msg : string(1..length);
>
> msg(1) := '~'; -- how do I get the asci value for this character?
> msg(2) := 33;
> msg(3) : msg(1)'value + msg(2)'value;
>
> this should more or less add the ascii value of the first character
with
> the value of the second.
>
I do this all the time, in the following way:
Msg : String (1 .. Length);
type Msg_Bytes is array (Msg'Range)of Interfaces.C.Unsigned_Char;
use type Interfaces.C.Unsigned_Char;
Msg_Overlay : Msg_Bytes;
for Msg_Overlay'Address use Msg'Address;
Now, you have Msg_Overlay, an array of numeric objects that can be
maniplulated both mathematically and logically (since Unsigned_Char
is a modular type), and any results written to any components
of that array will show up as the appropriate Character values in Msg.


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-04-03  0:00   ` Robert Dewar
@ 2000-04-04  0:00     ` Robert A Duff
  2000-04-05  0:00       ` Robert Dewar
  0 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2000-04-04  0:00 UTC (permalink / raw)


Robert Dewar <robert_dewar@my-deja.com> writes:

> For example, in
> 
>    x : integer := f (x);
>    y : access_to_integer;
>    for y'address use x'address;
> 
> the declaration of y will likely clobber the initial value
> of x, ...

I think it is *required* to clobber the initial value of x,
isn't it?  Because y has a default initial value.

- Bob




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-04-03  0:00   ` Robert Dewar
@ 2000-04-04  0:00     ` Mats Weber
  2000-04-05  0:00       ` Robert Dewar
  0 siblings, 1 reply; 9+ messages in thread
From: Mats Weber @ 2000-04-04  0:00 UTC (permalink / raw)


Robert Dewar wrote:

> One more trouble with the above declarations is that there
> is no guarantee that the address of an array is necessarily
> the address of the first element. The above declarations
> may simply result in overlaying dope vectors with peculiar
> results!

This should not be a problem anymore with Ada 95, as there is an
implementation advice saying that an array's 'Address should point to
the contents, not the dope.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-04-04  0:00     ` Mats Weber
@ 2000-04-05  0:00       ` Robert Dewar
  0 siblings, 0 replies; 9+ messages in thread
From: Robert Dewar @ 2000-04-05  0:00 UTC (permalink / raw)


In article <38EA18EE.1C3D9D2F@mail.com>,
  Mats Weber <matsw@mail.com> wrote:
> This should not be a problem anymore with Ada 95, as there is
an
> implementation advice saying that an array's 'Address should
point to
> the contents, not the dope.


Implementation advice is not a guarantee, and this particular
implementation advice is not practical in some implementation
environments.

It is not at ALL clear that this can apply to an address clause
anyway in an implementation which puts dope behind the array
(think about the consequences!)



Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Character/Integer Conversion
  2000-04-04  0:00     ` Robert A Duff
@ 2000-04-05  0:00       ` Robert Dewar
  0 siblings, 0 replies; 9+ messages in thread
From: Robert Dewar @ 2000-04-05  0:00 UTC (permalink / raw)


In article <wcc66txkepv.fsf@world.std.com>,
  Robert A Duff <bobduff@world.std.com> wrote:

> I think it is *required* to clobber the initial value of x,
> isn't it?  Because y has a default initial value.

Not quite, I can describe a model in which integers are
all designed to look like null addresses by default. Sure
it is unlikely, which is why I used the word "likely" in
my description.

Likely is a semi-technical term for me in this context, it
means that in practice x is true even if x cannot be formally
deduced from the RM :-)


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2000-04-05  0:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-31  0:00 Character/Integer Conversion ebresie
2000-04-02  0:00 ` DuckE
2000-04-03  0:00 ` David
2000-04-03  0:00   ` Robert Dewar
2000-04-04  0:00     ` Robert A Duff
2000-04-05  0:00       ` Robert Dewar
2000-04-03  0:00   ` Robert Dewar
2000-04-04  0:00     ` Mats Weber
2000-04-05  0:00       ` Robert Dewar

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