2015-11-11 42 views

回答

0

只需Move()輸入變量入適當大小的一個字節數組,例如:

var 
    value: Smallint; 
    arr: array[0..SizeOf(Smallint)-1] of Byte; 
begin 
    value := ...; 
    Move(value, arr[0], SizeOf(value)); 
end; 

或者:

type 
    TBytes = array of Byte; 

function GetBytes(value: Smallint): TBytes; 
begin 
    SetLength(Result, SizeOf(value)); 
    Move(value, Result[0], SizeOf(value)); 
end; 

var 
    value: Smallint; 
    arr: TBytes; 
begin 
    value := ...; 
    arr := GetBytes(value); 
end; 
1

可以使用一個簡單的轉換。使用相同類型的雷米,看起來像這樣:

var 
    value: Smallint; 
    arr: array[0..SizeOf(Smallint)-1] of Byte; 
begin 
    value := ...; 
    // by value cast: 
    Smallint(arr) := value; 
    // or by address cast: 
    PSmallint(@arr)^ := value; 
end; 

或本:

var 
    value: Smallint; 
    arr: TBytes; 
begin 
    value := ...; 
    SetLength(arr, SizeOf(value)); 
    PSmallint(arr)^ := value; 
end;