Ada:将记录展平到字节数组
我有一张较小的唱片,总共有16位大小Ada:将记录展平到字节数组,ada,Ada,我有一张较小的唱片,总共有16位大小 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 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;
如何将“theu记录”展平为字节数组或类似的内容
谢谢大家! 您始终可以使用
未检查的\u转换
,但是,这种方法是未检查的,因此您告诉编译器(a)您知道自己在做什么,以及(b)它不能帮助您[否则它将被命名为检查的\u转换]
另一种转换方法是叠加,这是我的首选方法(如果不是简单的类型重命名问题),如果情况发生变化,则可以根据需要修改转换函数
-- 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;
——子类型重命名。
子类型字节为Interfaces.Unsigned_8;
--用于转换为字节集合的常规类型。
类型Byte_Array是字节的数组(正范围);
--从_记录中指定字节的集合。
子类型记录\字节是字节\数组(1..u记录'大小/字节'大小);
--正在将记录转换为字节。。。
函数转换(输入:_记录)返回记录_字节为
结果:常量记录_字节;
对于结果“地址使用输入”地址;
Pragma导入(约定=>Ada,实体=>Result);
开始
返回结果;
端转换;
--正在将字节转换为记录。。。在Ada 2012中!
函数转换(输入:记录\u字节)返回\u记录为
结果:使用
导入,约定=>Ada,地址=>Input'Address;
开始
返回结果;
端转换;
另一种可能更好的方法(如果您正在尝试序列化,我想是这样)是创建读/写函数,并覆盖默认的
'read
和'write
属性。但是,您可以始终使用未选中的转换,这种方法是未经检查的,因此您告诉编译器(a)您知道自己在做什么,以及(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;
——子类型重命名。
子类型字节为Interfaces.Unsigned_8;
--用于转换为字节集合的常规类型。
类型Byte_Array是字节的数组(正范围);
--从_记录中指定字节的集合。
子类型记录\字节是字节\数组(1..u记录'大小/字节'大小);
--正在将记录转换为字节。。。
函数转换(输入:_记录)返回记录_字节为
结果:常量记录_字节;
对于结果“地址使用输入”地址;
Pragma导入(约定=>Ada,实体=>Result);
开始
返回结果;
端转换;
--正在将字节转换为记录。。。在Ada 2012中!
函数转换(输入:记录\u字节)返回\u记录为
结果:使用
导入,约定=>Ada,地址=>Input'Address;
开始
返回结果;
端转换;
另一种可能更好的方法(如果您正在尝试序列化。正如我所怀疑的那样)是创建读/写函数,并覆盖默认的'read
和'write
属性。这是如何使用未选中转换方式的示例:
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;
取决于您打算做什么,覆盖和未检查的转换
方式都会有与之相关的优点和缺点:)在如何使用未检查的转换
方式的示例中:
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;
取决于您打算做什么,覆盖和未检查的转换方式都有与之相关的优点和缺点:)您能说出为什么需要字节数组吗?因为显然,记录在某种意义上已经是一个(2元素)字节数组。我怀疑他在尝试某种形式的序列化。一些标准坚持消息的“数据”部分(数据序列化)表示为字节数组。。。因此,一些实现会模仿这一点来安抚monkeys.NWS,如果没有端点或其他表示问题,则通常可以同样轻松地导入具有所需数据类型的发送和接收函数。另一种覆盖方式!你能说一下为什么需要一个字节数组吗?因为显然,记录在某种意义上已经是一个(2元素)字节数组。我怀疑他在尝试某种形式的序列化。一些标准坚持消息的“数据”部分(数据序列化)表示为字节数组。。。因此,一些实现会模仿这一点来安抚monkeys.NWS,如果没有端点或其他表示问题,则通常可以同样轻松地导入具有所需数据类型的发送和接收函数。另一种覆盖方式!在计算U8\u记录\u数组的长度时,需要进行取整。。(U8记录'Size+U8'Size-1)/U8'Size
。在计算U8记录\u数组的长度时,需要进行取整。。(记录'Size+U8'Size-1)/U8'Size
。