Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-05

Copying a Pchar to an array of bytes

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Reply by email

Alexander Grotewohl
to Giuliano Colla: I'm not too familiar with Pchar, and apparently I'm missing something. I have a Pchar…

I believe pchar has special treatment where pchar[i] is the same as pchar^[i]

do move(Hello^, ...

Alexander Grotewohl https://dcclost.com ________________________________
Earlier message 33 lines
From: fpc-pascal on behalf of Giuliano Colla Sent: Tuesday, May 19, 2020 1:05:54 PM To: FPC-Pascal users discussions 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.

Reply by email