Copying a Pchar to an array of bytes

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Copying a Pchar to an array of bytes

Giuliano Colla
I'm not too familiar with Pchar, and apparently I'm missing something.

I have a Pchar string which I must copy into an array of bytes.

Could someone explain me while a move doesn't work while an assignment
byte by byte does?

Here's a snippet of the code:

buffer: array [0..1023] of byte;
Hello: PChar = 'Hello from server';
....
   len := strlen(Hello);
----
Move(Hello,buffer,len); <--- Garbage in buffer - doesn't work
----
   for I := 0 to Pred(len) do
   begin
     buffer[I] := Byte(Hello[I]);  <---- Works just fine.
   end;

I fail to understand why. What I'm missing?

Giuliano

--
Do not do to others as you would have them do to you.They might have different tastes.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Copying a Pchar to an array of bytes

Luca Olivetti-2
El 19/5/20 a les 19:05, Giuliano Colla ha escrit:
> Move(Hello,buffer,len); <--- Garbage in buffer - doesn't work

Move(Hello^,buffer,len)

Bye
--
Luca
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Copying a Pchar to an array of bytes

Alexander Grotewohl
In reply to this post by Giuliano Colla
I believe pchar has special treatment where pchar[i] is the same as pchar^[i]

do move(Hello^, ...

--
Alexander Grotewohl


From: fpc-pascal <[hidden email]> on behalf of Giuliano Colla <[hidden email]>
Sent: Tuesday, May 19, 2020 1:05:54 PM
To: FPC-Pascal users discussions <[hidden email]>
Subject: [fpc-pascal] Copying a Pchar to an array of bytes
 
I'm not too familiar with Pchar, and apparently I'm missing something.

I have a Pchar string which I must copy into an array of bytes.

Could someone explain me while a move doesn't work while an assignment
byte by byte does?

Here's a snippet of the code:

buffer: array [0..1023] of byte;
Hello: PChar = 'Hello from server';
....
   len := strlen(Hello);
----
Move(Hello,buffer,len); <--- Garbage in buffer - doesn't work
----
   for I := 0 to Pred(len) do
   begin
     buffer[I] := Byte(Hello[I]);  <---- Works just fine.
   end;

I fail to understand why. What I'm missing?

Giuliano

--
Do not do to others as you would have them do to you.They might have different tastes.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Copying a Pchar to an array of bytes

Giuliano Colla
Thank you both guys.


Il 19/05/2020 19:28, Alexander Grotewohl ha scritto:
> do move(Hello^, ...

--
Do not do to others as you would have them do to you.They might have different tastes.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Copying a Pchar to an array of bytes

Free Pascal - General mailing list
In reply to this post by Luca Olivetti-2
On Tue, 19 May 2020 19:22:55 +0200, Luca Olivetti
<[hidden email]> wrote:

>Move(Hello^,buffer,len)

or:
 Move(Hello[0],buffer,len);


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal