Board index » delphi » Passing chars to strings

Passing chars to strings

Hi, I've been wondering how to do this simple task in Pascal lately:
I want to make a string (teststring: string) equal to a character
array (testchar: array [1..255] of char). I can't use the statement
teststring := testchar because this is a type mismatch error.How do I
go about passing the character array data into the string? Do I have
to use pointers or some special type?
Thanks for any help. E-mail or follow-up posting is fine. BTW, I am
using TPW 1.5.

+--------------------------------------------------------------------------+
 Internet: mahoneyjm98%cs25%us...@cadetmail.usafa.af.mil
 Commodore 64 rules!                     I don't care who takes my views...
 "Kill one man, you're a {*word*190}er; Kill many men, you're a conqueror;
  Kill them all, you're a God."
 Various handles: -=Gumbytwo=- -=Flash=- -=The Cookie Monster=-
+--------------------------------------------------------------------------+

 

Re:Passing chars to strings


Simply use the STRPAS() function, from the STRINGS unit.
If the char part is "mychar:array[0..100] of char" and the string part
is "mystring:string" then,

mystring:=strpas(mychar);

and to go the other way :-

strpcopy(mystring,mychar);

--
Alistair Galbraith

Re:Passing chars to strings


Quote
>I want to make a string (teststring: string) equal to a character
>array (testchar: array [1..255] of char). I can't use the statement
>teststring := testchar because this is a type mismatch error.How do I
>go about passing the character array data into the string? Do I have
>to use pointers or some special type?

I am writing this right now and have NOT debugged it, but I see no reason
why it wouldn't work.

=========================================================================
FUNCTION String_It (  testchar  :  ARRAY [1..255] OF CHAR  ;
                      testsize  :  INTEGER                ):  STRING;

   VAR
      i  :  INTEGER;

   BEGIN  {String_It}
      i := 1;
      WHILE (i <= 255) AND (i <= testsize) DO    
         BEGIN
            String_It[i] := testchar[i];
            i:= i + 1;
         END;
      String_It[0] := CHAR(i-1);            {0 holds the length position}
   END;   {String_It}
=========================================================================

Hope that helps you.

Hugh

Re:Passing chars to strings


Hugh J. O'Donnell (c551...@mizzou1.missouri.edu) wrote:
: >I want to make a string (teststring: string) equal to a character
: >array (testchar: array [1..255] of char). I can't use the statement
: >teststring := testchar because this is a type mismatch error.How do I
: >go about passing the character array data into the string? Do I have
: >to use pointers or some special type?

Wouldn't it fail because teststring is an 80-length variable (type string
has a default length of 80), while testchar is equivalent to a 255 length
string?

Try putting VAR teststring:STRING[255];  
This should help.

Re:Passing chars to strings


Quote
>>go about passing the character array data into the string? Do I have
>>to use pointers or some special type?
>I am writing this right now and have NOT debugged it, but I see no reason
>why it wouldn't work.
>=========================================================================
>FUNCTION String_It (  testchar  :  ARRAY [1..255] OF CHAR  ;
>                      testsize  :  INTEGER                ):  STRING;

 Wouldn't it be easier and faster to just use :

   MOVE(TestString[1],TestChar[0],ArraySize);
   TestString[0] := ArraySize;

Re:Passing chars to strings


Quote
bkta...@aol.com (BKTahoe) wrote:
>In article <3s2255$...@usafa2.usafa.af.mil>,
>mahoneyjm98%cs25%us...@cadetmail.usafa.af.mil (Justin M. Mahoney) writes:
>>I want to make a string (teststring: string) equal to a character
>>array (testchar: array [1..255] of char). I can't use the statement
>>teststring := testchar because this is a type mismatch error.
>Try this :
>Move(TestChar,TestString[1],NumChars);
>s[0] := Chr(NumChars);
>BK

Wow--thanks for all the help, including those that have e-mailed me.
Now I have about a million ways to do it.
Never knew Pascal was so versatile... maybe it isn't so bad after
all...
Thanks again for all the help!

+--------------------------------------------------------------------------+
 Internet: mahoneyjm98%cs25%us...@cadetmail.usafa.af.mil
 Commodore 64 rules!                     I don't care who takes my views...
 "Kill one man, you're a {*word*190}er; Kill many men, you're a conqueror;
  Kill them all, you're a God."
 Various handles: -=Gumbytwo=- -=Flash=- -=The Cookie Monster=-
+--------------------------------------------------------------------------+

Re:Passing chars to strings


In article <3s2255$...@usafa2.usafa.af.mil>,
   mahoneyjm98%cs25%us...@cadetmail.usafa.af.mil (Justin M. Mahoney) wrote:

Quote
>Hi, I've been wondering how to do this simple task in Pascal lately:
>I want to make a string (teststring: string) equal to a character
>array (testchar: array [1..255] of char). I can't use the statement
>teststring := testchar because this is a type mismatch error.How do I
>go about passing the character array data into the string? Do I have
>to use pointers or some special type?
>Thanks for any help. E-mail or follow-up posting is fine. BTW, I am
>using TPW 1.5.

When you mention TPW I get a bit uncertain, but in TP5.5 it is perfectly
allright to use the ABSOLUTE statement.

Maybe this is real mode only, but this statement makes it possible to
map two variables in the same memoryspace, and this is just the thing we
want in a situation like this.

Take this code as an explanation:

var     b:byte;
        s:string;
        a:array[0..255] of char absolute s;
begin
  s:='test';
  writeln(s);
  a[0]:=#10;
  for b:=1 to 10 do a[b]:=chr(b+64);
  writeln(s);
end.

The output of this is:

        test
        ABCDEFGHIJ

Of course this works both ways, thus if either of the variables are changed
the other will immediately be altered too!

This method is also quite useful for direct addressing of the screen,
since the ABSOLUTE statement can take either a variable or a memory address.

 Greetings types Jonas Eriksson, who will program HTML for food or whatever...
/----------- Thought of the day ---------------------------------------------\
|   "For every action, there is an equal but opposite critical analysis."    |
\-------------------------------------------------- Solomon Short -----------/
Mail:jonas.eriks...@lpul.slu.se              Talk:jo...@anordkvist.lpul.slu.se
Tel:018-672501         http://jeriksson.lpul.slu.se/jonas/jonas.htm (when up)

Re:Passing chars to strings


pin...@ccnet.com had an accident with a keyboard and wrote:
: Wouldn't it fail because teststring is an 80-length variable (type string
: has a default length of 80), while testchar is equivalent to a 255 length
: string?
I beg to differ, if you don't specify a length then enough space is
allocated for 255 chars.

--
Mike Weavind       : Best way to accelerate a Mac is at 9.8m/s^2
cee...@cee.hw.ac.uk:

Re:Passing chars to strings


Quote
Pinole Library (pin...@ccnet.com) wrote:

: Wouldn't it fail because teststring is an 80-length variable (type string
: has a default length of 80), while testchar is equivalent to a 255 length
: string?

: Try putting VAR teststring:STRING[255];  
: This should help.

        No.  This is misleading and incorrect.  An array of characters is
_not_ compatible with a string in that you cannot assign an array of char
into a string.  To copy a string into an array of characters, either do a
"Move" into the 1st byte of the string (and make sure you set the 0th or
length byte of the string correctly) or do a StrCopy (supported in BP7 and
newer).

Mike.

--
====================================================================
Mike Frisch                           Email: mfri...@saturn.tlug.org
Northstar Technologies                        Compuserve: 76620,2534
Newmarket, Ontario, CANADA

Re:Passing chars to strings


Sorry about the redundant posting. AOL was having network problems last
night and my repeated sessions were aborting but the software was
evidently sending old outgoing replies successfully. Ooops. BK

Re:Passing chars to strings


Quote
>Wouldn't it fail because teststring is an 80-length variable (type string
>has a default length of 80), while testchar is equivalent to a 255 length

         ^^^^^^^^^^^^^^^^^^^^^^^^^^^

 Actually a STRING type defaults to a full 255 chars+ 1 char for length
storage for a total of 256 bytes.

 To demonstrate this just try running:

 BEGIN
   Writeln(SizeOf(STRING));
 END.

Re:Passing chars to strings


In article: <3stdtm$...@vixa.voyager.net>  wiz...@voyager.net (Warren Wolfe) writes:

[snip]

Quote
> Once you get the 255 characters into the string, it's up to you to clean
> up the garbage that is likely at the end of the useful information.  If
> it's a 'C' language type, 00H terminated string, that makes it pretty
> easy.  If it's space filled at one end or the other, you can use a loop
> to remove spaces at the end.  It can get ugly, and the way that's best in
> your case will depend on just what kind of data you're getting in the
> character arrays.

It need not be so complicated. Simply put the line

teststring := '';

in before the for line!
--
---------------------------------------------------------------------------
| Matt Thornfield    EMail ma...@meeker.demon.co.uk     Leeds,England     |
| If at first you don't succeed, reboot and try again                     |
---------------------------------------------------------------------------

Re:Passing chars to strings


Justin M. Mahoney (mahoneyjm98%cs25%us...@cadetmail.usafa.af.mil) wrote:
: Hi, I've been wondering how to do this simple task in Pascal lately:
: I want to make a string (teststring: string) equal to a character
: array (testchar: array [1..255] of char). I can't use the statement
: teststring := testchar because this is a type mismatch error.How do I
: go about passing the character array data into the string? Do I have
: to use pointers or some special type?

        It's annoying, but you have to deal with it character by
character.  I do a lot of this.  Here's a sample statement:

     for loop := 1 to 255 do teststring [loop] := testchar [loop];

Once you get the 255 characters into the string, it's up to you to clean
up the garbage that is likely at the end of the useful information.  If
it's a 'C' language type, 00H terminated string, that makes it pretty
easy.  If it's space filled at one end or the other, you can use a loop
to remove spaces at the end.  It can get ugly, and the way that's best in
your case will depend on just what kind of data you're getting in the
character arrays.  If you need to reply, please do so via e-mail.

                        Peace,
                        Warren

Re:Passing chars to strings


Quote
In article <3stdtm$...@vixa.voyager.net> wiz...@voyager.net (Warren Wolfe) writes:
>Justin M. Mahoney (mahoneyjm98%cs25%us...@cadetmail.usafa.af.mil) wrote:
>: Hi, I've been wondering how to do this simple task in Pascal lately:
>: I want to make a string (teststring: string) equal to a character
>: array (testchar: array [1..255] of char). I can't use the statement
>: teststring := testchar because this is a type mismatch error.How do I
>: go about passing the character array data into the string? Do I have
>: to use pointers or some special type?
>        It's annoying, but you have to deal with it character by
>character.  I do a lot of this.  Here's a sample statement:
>     for loop := 1 to 255 do teststring [loop] := testchar [loop];

move(testchar[1], teststring[1], 255) ; then set teststring[0] to correct
value ;
but use Low & Sizeof instead of the Teststring numbers.  Should work; see
online help.
--
John Stockton : J...@dclf.npl.co.uk from off-site.   MIME.   WP.
 National Physical Laboratory, Teddington, Middlesex, TW11 0LW, UK
  Direct Phone +44 181-943 6087, Nearby Fax +44 181-943 7138  
** Postings out, Email in/out are fast; News in is often slow. **
Offshore news takes 0..10+ days to arrive; please mail copies of
replies!  Regret system puts unzoned (UK civil) time on messages.

Re:Passing chars to strings


Quote
wiz...@voyager.net (Warren Wolfe) writes:
>Justin M. Mahoney (mahoneyjm98%cs25%us...@cadetmail.usafa.af.mil) wrote:
>: Hi, I've been wondering how to do this simple task in Pascal lately:
>: I want to make a string (teststring: string) equal to a character
>: array (testchar: array [1..255] of char). I can't use the statement
>: teststring := testchar because this is a type mismatch error.How do I
>: go about passing the character array data into the string? Do I have
>: to use pointers or some special type?
>    It's annoying, but you have to deal with it character by
>character.  I do a lot of this.  Here's a sample statement:
>     for loop := 1 to 255 do teststring [loop] := testchar [loop];

Why so complicated?

What about this:

var
 ConvRec : record
           case boolean of
            false:(length:byte;data:array[0..255] of char);
            true: (strg:string);
           end;

{...}
 ConvReg.data:=testchar;
 ConvRec.length:=255; {or calculate the length, if necessary}
 teststring:=ConvRec.strg

Quote
>                    Peace,
>                    Warren

Ch. Eltschka
If you are missing information, just remember, that this article was moving
through the net with high relativistic speed, so any shortening comes from lorentz
contraction...

Other Threads