MemoryStream and constructing an array of bytes
delphi, memorystream
Solution
Is there a better way to construct an array of bytes than this?
That's a perfectly reasonable way to do it, in my view.
I see that in the stream is added the value $6F32 instead of $3E6C.
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.
Why do I get the first byte in the stream instead of the third?
Because the `Memory` property always refers to the beginning of the stream. It does not take account of the stream's current position.
Problem
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?