سؤال

  1. How to pass "a" variable into HostToNetwork() ? Need somehow convert it..
  2. And how to insert into "a" variable, this HEX code ?

    var
    a : array[0..19] of Shortint;
    h : string;
    
    h =: '714BD6D36D4944F4E4F943CB03D128EA5C372FF6';
    
    GStack.HostToNetwork(a)
    

from IdStack.pas:

function HostToNetwork(AValue: Word): Word; overload; virtual; abstract;
function HostToNetwork(AValue: LongWord): LongWord; overload; virtual; abstract;
function HostToNetwork(AValue: Int64): Int64; overload; virtual; abstract;
function HostToNetwork(const AValue: TIdIPv6Address): TIdIPv6Address; overload; virtual;

Thanks

EDIT:

    h := '714BD6D36D4944F4E4F943CB03D128EA5C372FF6';
    for x := 0 to 19 do
    begin
      a[x] := StrToInt('$'+Copy(h, x, 2));
    end;

correct ?

هل كانت مفيدة؟

المحلول

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top