Very generally speaking, reinterpreting the raw memory occupied by a variable of one type as if it belonged to a variable of another type is called
type-punning. This can easily be achieved using pointer casts,
however...
var
FooWord: UInt16;
FooByteA, FooByteB: UInt8;
begin
FooWord := $BEEF;
FooByteA := PUInt8(@FooWord)[0]; // $EF on little endian, $BE on big endian machines
FooByteB := PUInt8(@FooWord)[1];
end;
...this is not portable, mainly due to processor endianness. Worse still, in other languages such as C or C++ this is undefined behaviour (strict aliasing rule; dereferenced pointers' types must be compatible). In C (but not C++) this can be circumvented by using a
union, but the recommended standard method is indeed to just use
memcpy and rely on the optimizer to elide the function call. I tested the
Move() approach in FPC 3.2.2 with
-O3, but unfortunately the function call wasn't optimized out...
Anyhow, if you simply want to write words/dwords/qwords to a file in a machine-independent format, I'd recommend the
NtoBE/BEtoN family of functions (native-to-big-endian and vice-versa conversions):
Stream.WriteWord(NtoBE(FooWord));
FooWord := BEtoN(Stream.ReadWord());