2013-08-26 26 views
1

我有一個小型的記錄,總共有16位大小。Ada:Flatten記錄到字節數組

type Type_1 is (val1, val2, val3, val4, val5, val6, val7, val8); 
for Type_1 use (val1 => 0, val2 = 1, val3 = 2, val4 => 3 
       val5 => 4, val6 => 5, val7 => 6, val8 => 7); 
for Type_1'Size use 3; 

type Type_2 is (val1, val2); 
for Type_2 use (val1 => 0, val2 = 1); 
for Type_2'Size use 1; 

etc, etc 

type The_Record is record 
    element_1 : Type_1; 
    element_2 : Type_2; 
    element_3 : Type_3; 
    element_4 : Type_4; 
end record; 
for the_Record use record 
    element_1 at 0 range 0 .. 2; 
    element_2 at 0 range 3 .. 4; 
    element_3 at 0 range 5 .. 12; 
    element_4 at 0 range 13 .. 15; 
end record; 
for The_Record'Size use 16; 

我怎樣才能拉平「The_Record」成字節或類似的陣列?

謝謝!

+0

你能說*爲什麼*你需要一個字節數組?因爲顯然'The_Record'在某種意義上已經是一個(2元素)字節數組。 –

+0

我懷疑他正在嘗試某種形式的序列化 – Shark8

+0

一些標準堅持認爲消息(數據的序列化)的「數據」部分表示爲字節數組...因此,某些實現將模仿此來安撫[ISVV](http ://en.wikipedia.org/wiki/Independent_software_verification_and_validation)猴子。 – NWS

回答

3

您可以隨時使用Unchecked_conversion,但是,這種做法是選中,因此你告訴編譯器(一)你知道你在做什麼,和(b),它不能幫助你[否則會被命名爲checked_conversion]。

,你可以做轉換的另一種方法是用覆蓋,這是首選方法(如果它不是一個簡單的類型重命名問題),就好像事情,然後根據需要更改轉換功能可以修改。

-- Subtype-renaming. 
Subtype Byte is Interfaces.Unsigned_8; 
-- General type for conversion to a collection of bytes. 
Type Byte_Array is Array (Positive Range <>) of Byte; 
-- Speciffic collection of bytes from The_Record. 
Subtype Record_Bytes is Byte_Array(1..The_Record'Size/Byte'Size); 

-- Converting record to bytes... 
Function Convert(Input : The_Record) return Record_Bytes is 
    Result : Constant Record_Bytes; 
    For Result'Address use Input'Address; 
    Pragma Import(Convention => Ada, Entity => Result); 
begin 
    Return Result; 
end Convert; 

-- Converting bytes to record... in Ada 2012! 
Function Convert(Input : Record_Bytes) return The_Record is 
    Result : The_Record with 
      Import, Convention => Ada, Address => Input'Address; 
begin 
    Return Result; 
end Convert; 

另外,可能會更好,這樣(如果你想進行序列化。因爲我懷疑)的這樣做將是創建讀/寫功能和覆蓋默認'read'write屬性。

1

這在如何使用Unchecked Conversion方式的例子:

with Ada.Unchecked_Conversion; 
with Ada.Text_Io; 

procedure Uc is 

    type The_Record is record 
     A : Integer; 
     B : Integer; 
    end record; 

    -- define a byte sized thing... 
    type U8 is mod 2**8; 
    for U8'Size use 8; 

    -- have your array defined according to 
    -- the size of the record it needs to hold 
    type U8_Record_Array is array (1 .. The_Record'Size/U8'Size) of U8; 

    -- instantiate Unchecked_Conversion 
    function To_Array is new Ada.Unchecked_Conversion (Source => The_Record, 
                 Target => U8_Record_Array); 
    -- Declare a record and convert it to an array 
    R : constant The_Record := (A => 1, B => 2); 
    A : constant U8_Record_Array := To_Array (R); 

begin 
    -- Print the Array As Bytes 
    for I in A'Range loop 
     Ada.Text_Io.Put (U8'Image (A(I))); 
    end loop; 

end Uc; 

根據究竟你打算做什麼,無論是覆蓋& Unchecked Conversion方式將有專業人士與它們相關的&缺點: )

+1

在計算'U8_Record_Array' - '1 ..(The_Record'Size + U8'Size-1)/ U8'Size'的長度時,需要進行四捨五入。 –