I am using a MemoryStream to construct an array of bytes i need to send to a server.I have thre questions:
1) Is there a better way to construct an array of bytes than this ?
2) Why this pice of code writes bogus in my memory stream ?
var
serial : word;
MS : TMemoryStream;
const
somebytes : array [0..1] of byte = ($72,$72);
...
begin
MS := TMemoryStream.Create();
try
MS.Write(somebytes[0],2);
serial := $3E6C;
MS.Write(serial,2);
finally
MS.Free;
end;
Using the debugger i see that in the stream is added the value $6F32 instead of $3E6C.
3) If i call
MS.Position := 2;
and then i access PByte(MS.Memory)^ why do i get the first byte in the stream instead of the third?
That’s a perfectly reasonable way to do it, in my view.
Check again. The correct values are in fact added. But beware of the traps of little endian data types. The 4 bytes added to your stream, in order, are: $72, $72, $6C, $3E.
Because the
Memoryproperty always refers to the beginning of the stream. It does not take account of the stream’s current position.