2012-11-23 52 views
0
  1. 如何將「a」變量傳遞給HostToNetwork()?需要以某種方式轉換它..
  2. 以及如何插入「一個」變量,這個十六進制代碼?GStack.HostToNetwork輸入類型 - Shortint?

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

從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; 

感謝

編輯:

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

是否正確?

+0

你打算怎麼辦?自從您列出的['HostToNetwork'](http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=%21%21OVERLOADED_HostToNetwork_TIdStack.html)「方法都是抽象的,最後一個就是第一個超載。所以簡而言之,它們在['TIdStack'](http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=TIdStack.html)基類中完全沒有。 – TLama

+0

即時使用HostToNetwork將1字節大小的數據轉換成我的傳遞數據(int/int64,現在需要shortint)爲「big endian」 – waza123

+0

@ waza123,由於內存中的表示形式相同,所以字節永久性沒有意義! – jachguate

回答

2

Shortint大小爲1個字節,因此根本不受endian的影響。 GStack.HostToNetwork()GStack.NetworkToHost()方法僅對多字節整數進行操作。所以在這個例子中使用GStack方法沒有意義。您正在聲明一個20個原始字節的數組。您需要首先將這些字節拆分爲整數,然後再轉換它們。一旦你將它們分開,那麼你可以根據需要將它們分別傳遞給GStack

根據您的earlier question,您實際上處理整數記錄,而不是原始字節。您需要將整數轉換爲網絡字節順序之前您將記錄編碼爲原始字節,然後將整數轉換爲主機字節順序AFTER您首先將原始字節解碼爲記錄。例如:

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;