comp.lang.ada
 help / color / mirror / Atom feed
* "Integer" value of a CHARACTER
@ 1990-11-28 21:08 Paul Stachour
  1990-11-29 20:52 ` Mike Murphy
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Stachour @ 1990-11-28 21:08 UTC (permalink / raw)



  I have this "simple" problem.  I have something that I need
its internal representation of.  I need this to calculate a
checksum or a hash-value or ...  I really don't even care what
the value is, or plan to do anything neat-o with it.  I just
need it to be repeatable.
  In the (simplified) example below, I am trying to get a hash-value
from several characters in a string.  However, I seem to get problems
in doing this.  In the example given, I get "constraint-error" on the
attempt to do the "conversion". (V_Name is a 60-char string with an
OK name in it, the first character is a "c".)

  What am I doing wrong?  How should I be doing it?
[Unchecked conversion between an 8-bit character and an
integer is not guarenteed either, is it?  Or do I work
with some particular range of integer?]

-- ********************************************************** --
function Hash_Name(
    The_Name                          : in V_Name ) 
  return Positive is
  C:                                    STRING(1..1);
begin
   C(1) := The_Name(1);
   return POSITIVE'VALUE(C(1..1));
--1      return POSITIVE'VALUE(The_Name(1));
end;

-- ********************************************************** --

-- 
Paul Stachour         Secure Computing Technology Corp
stachour@sctc.com      1210 W. County Rd E, Suite 100           
		 	   Arden Hills, MN  55112
                             [1]-(612) 482-7467

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

* Re: "Integer" value of a CHARACTER
  1990-11-28 21:08 "Integer" value of a CHARACTER Paul Stachour
@ 1990-11-29 20:52 ` Mike Murphy
  0 siblings, 0 replies; 2+ messages in thread
From: Mike Murphy @ 1990-11-29 20:52 UTC (permalink / raw)


In article <1990Nov28.210856.14960@sctc.com> stachour@sctc.com (Paul Stachour) writes:
>
>  In the (simplified) example below, I am trying to get a hash-value
>from several characters in a string.  However, I seem to get problems
>in doing this.  In the example given, I get "constraint-error" on the
>attempt to do the "conversion". (V_Name is a 60-char string with an
>OK name in it, the first character is a "c".)
>
>-- ********************************************************** --
>function Hash_Name(
>    The_Name                          : in V_Name ) 
>  return Positive is
>  C:                                    STRING(1..1);
>begin
>   C(1) := The_Name(1);
>   return POSITIVE'VALUE(C(1..1));
>--1      return POSITIVE'VALUE(The_Name(1));
>end;
>-- ********************************************************** --

The type'VALUE(string) attribute returns the type-value that the
string image represents.  Thus POSITIVE'VALUE("12") = 12, 
while POSITIVE'VALUE("cat") raises constraint_error, because "cat"
is not the image of a positive number.  To get a hash value,
you want to use the 'POS attribute, e.g.
	CHARACTER'POS(The_Name(1)) = character'pos('c') = 99
To get a real hash value you probably want to do some arithmetic
on the 'pos values of more than one character in the string.

If you want to use unchecked_conversion, you could convert four
characters at a time to integer (assuming 4-byte integers), or
convert one character to a tiny_integer (or whatever 1-byte integer
your implementation defines):
	function to_int is new unchecked_conversion (string, integer);
	function to_tint is new unchecked_conversion (character, tiny_integer);
	integer_obj := to_int(The_Name(1..4));
	tiny_integer_obj := to_tint(The_Name(1));
		-- this is essentially the same as doing a 'pos.

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

end of thread, other threads:[~1990-11-29 20:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1990-11-28 21:08 "Integer" value of a CHARACTER Paul Stachour
1990-11-29 20:52 ` Mike Murphy

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