[comp.lang.pascal] Converting Strings to Char

dragon@westfort.UUCP (The Mystic) (02/16/89)

        A program that I'm currently working w/ via the communications port, 
modem ectal, a routine within it sends characters out in the form of CHAR, as 
the user types on the keyboard.. I've wanted to make use of this routine to 
send strings across, yet there's a conflict in my methods.. Seeing as I can't 
make a string of VAR form CHAR, I make it a string[80], and then using the 
copy command, take it apart one character at a time into a string[1]; and try 
to pass it off into the send routine.. I havn't been able to find a way to 
bypass the var type checking, or convert the string into a character form in 
order for me to do this.. I'm using Turbo Pascal 5.0..  Any help would be 
appreciated.. The program I'm using uses the x00.sys driver for it's I/O.. A 
sample program would be...:

procedure send(ch:char);

var t:registers;
begin
  t.ah:=$0b;
  t.al:=ord(ch);
  foss(t);
 end;

procedure test;

var
  x:integer;
  s:string[80];
  sd:string[1];
  
begin
  s:='Hello';
  for x:=1 to length(s) do begin
    sd:=copy(s,x,1);
    send(sd);    (*  The problem.. Can't send a string through as a char.. *)
   end;
  end;

begin
test;
end.

        Jason

RDK%vm.temple.edu@cunyvm.cuny.edu (Robert Keiser) (02/18/89)

A string of size 1 and a char are not the same size.  It is easiest to think
of a variable defined as a string[n] to be a packed array [0..n] of Char.
the 0th element contains the size of the string in character form.  You can
get the length of the string by using the ORD function. i.e. ORD(string[0])
is equal to the length of the string. (thats what the Length function does.)

So, a string of size 1 is really 2 elements long while a CHAR is only one
element.

I don't know if this helps but it was the first thing that caught my eye.


Robert Keiser
Temple University Computer Activities

Bitnet   : RDK@Templevm
Internet : RDK@VM.TEMPLE.EDU
US Mail  : Temple University
           Philadelphia, PA 19122

Mannie@cup.portal.com (William Allison Guynes) (02/18/89)

   Change this:

for x:=1 to length(s) do begin
  sd:=copy(s,x,1);
  send(sd);
end

   To this:

for x:= 1 to length(s) do
  send(s[x]);

This should work.  This just yanks out the character from location X.  You
can do completely away with the "sd" variable.
   Mannie

thurn@spruce.cis.ohio-state.edu (Martin Thurn) (02/19/89)

In article <1701@westfort.UUCP> westfort!dragon@tut.cis.ohio-state.edu writes:

>procedure send(ch:char);
>
>begin...end;
>
>var
>  x:integer;
>  s:string[80];
>  sd:string[1];
>  
>begin
>  s:='Hello';
>  for x:=1 to length(s) do begin
>    sd:=copy(s,x,1);
>    send(sd);    (*  The problem.. Can't send a string through as a char.. *)
>   end;
>  end;
>...

In TP 5.0, char just "ain't nohow" compatible with string (a minor change 
from earlier versions, I believe).  If procedure send() expects a char, 
you have to give it a char and nothing but a char:

var
  x: integer;
  s: string[80];
  c: char;

begin
  s := 'Hello';
  for  x := 1 to length(s)  do
    begin
      c := s[x];   (* string indexed with [] returns char at that position  *)
      send(c);
    end;
end;

---Martin Thurn         thurn@cis.ohio-state.edu
______________________________________________________________________________
"Heaven is an American salary,    | Hell is a Chinese  salary, 
            a Chinese  cook,      |        an English  cook,
           an English  house, and |         a Japanese house, and 
            a Japanese wife.      |        an American wife." - James Kabbler 
-=-

---Martin Thurn         thurn@cis.ohio-state.edu
______________________________________________________________________________
"Heaven is an American salary,    | Hell is a Chinese  salary, 

abcscnuk@csuna.UUCP (Naoto Kimura) (02/19/89)

In article <1701@westfort.UUCP> westfort!dragon@tut.cis.ohio-state.edu writes:
]  ... (text deleted) ...
] 
] procedure send(ch:char);
] 
]  ... (text deleted) ...
] 
] procedure test;
] 
] var
]   x:integer;
]   s:string[80];
   { structurally equivalent to: packed array [0..80] of char
     index 0 is length }
]   sd:string[1];
   { structurally equivalent to: packed array [0..1] of char
     index 0 is length }
]   
] begin
]   s:='Hello';
]   for x:=1 to length(s) do begin
]  {    sd:=copy(s,x,1);
]     send(sd);    (*  The problem.. Can't send a string through as a char.. *)
   }  send(s[x])
   {  ^^^^^^^^^^  }
]    end;
]   end;
] 
] begin
] test;
] end.
] 
]         Jason


                //-n-\\				Naoto Kimura
        _____---=======---_____			(csun!csuna!abcscnuk)
    ====____\   /.. ..\   /____====
  //         ---\__O__/---         \\	Enterprise... Surrender or we'll
  \_\                             /_/	send back your *&^$% tribbles !!

ncsmith@ndsuvax.UUCP (Timothy Smith) (02/22/89)

In article <1701@westfort.UUCP> westfort!dragon@tut.cis.ohio-state.edu writes:
>
>        A program that I'm currently working w/ via the communications port,
>modem ectal, a routine within it sends characters out in the form of CHAR, as

[stuff about disassembling a string type into a series of char type]

    Rather than using copy to take apart your string you can access
  it as an array structure.

           c := copy(s,1,1) is same as c := s[1]

  This will return a char type in c if c is defined as char or it will
  return a string type in c if c is defined as type string.

--
Tim Smith     North Dakota State University,  Fargo, ND  58105
UUCP:         ...!uunet!ndsuvax!ncsmith | 90% of the people on this planet
BITNET:       ncsmith@ndsuvax.bitnet    | are crazy and the rest of us are
INTERNET:     ncsmith@plains.NoDak.edu  | in grave danger of contamination



#! rnews           1

leonard@bucket.UUCP (Leonard Erickson) (03/01/89)

In article <1701@westfort.UUCP> westfort!dragon@tut.cis.ohio-state.edu writes:
<procedure send(ch:char);
<
<var t:registers;
<begin
<  t.ah:=$0b;
<  t.al:=ord(ch);
<  foss(t);
< end;
<
<  for x:=1 to length(s) do begin
<    sd:=copy(s,x,1);
<    send(sd);    (*  The problem.. Can't send a string through as a char.. *)
<   end;
<  end;

Try this:
  for x := 1 to length(s) do
    send(s[x]);

Always remember, strings are arrays!
-- 
Leonard Erickson		...!tektronix!reed!percival!bucket!leonard
CIS: [70465,203]
"I'm all in favor of keeping dangerous weapons out of the hands of fools.
Let's start with typewriters." -- Solomon Short