Shortint
is 1 byte in size, and as such is not affected by endian at all. The GStack.HostToNetwork()
and GStack.NetworkToHost()
methods only operate on multi-byte integers instead. So it does not make sense to use the GStack
methods in this example. You are declaring an array of 20 raw bytes. You need to first split those bytes into integers before you can then convert them. Once you have split them, then you can pass them to GStack
individually as needed.
Based on your earlier question, you are actualy dealing with records of integers, not raw bytes. You need to convert the integers to network byte order BEFORE you encode a record to raw bytes, and then convert the integers to host byte order AFTER you have first decoded raw bytes into a record. For example:
type
Tconnecting = packed record
a: int64;
b: integer;
c: integer;
end;
var
packet: Tconnecting;
send_data: TIdBytes;
begin
packet.a := $1234567890;
packet.b := 0;
packet.c := RandomRange(1, 9999999);
packet.a := GStack.HostToNetwork(packet.a);
packet.b := Integer(GStack.HostToNetwork(LongWord(packet.b)));
packet.c := Integer(GStack.HostToNetwork(LongWord(packet.c)));
send_data := RawToBytes(packet, SizeOf(packet));
udp.SendBuffer(send_data);
end;
.
var
Treply = packed record
c: integer;
b: integer;
a: int64;
end;
var
packet: Tconnecting;
received_data: TIdBytes;
begin
SetLength(received_data, SizeOf(Treply));
if udp.ReceiveBuffer(received_data) = SizeOf(Treply) then
begin
BytesToRaw(received_data, packet, SizeOf(Treply));
packet.a := GStack.NetworkToHost(packet.a);
packet.b := Integer(GStack.NetworkToHost(LongWord(packet.b)));
packet.c := Integer(GStack.NetworkToHost(LongWord(packet.c)));
// use packet as needed ...
end;
end;