我是一名 C 程序员,几周前开始学习 Ada。我一直对 Ada 如何处理外部二进制数据感到困惑,例如在解码存储在串行输入缓冲区中的通信数据包时。
在 C 中,我将定义一个打包结构来反映数据包的布局,然后将指向缓冲区的指针转换为指向结构的指针以访问通信数据中的各个元素。在 Ada 中进行这种解码的典型方法是什么?
我尝试使用以下方法在 Ada 中为 C 复制相同的方法
-- Fundamental types for fields in the packet
type Station_Addr_Type is mod 2**8;
type Func_Code_Type is (FUNC1, FUNC2);
for Func_Code_Type use
(
FUNC1 => 1,
FUNC2 => 2
);
Run Code Online (Sandbox Code Playgroud)
type Packet is
record
Station_Addr : Station_Addr_Type;
Func_Code : Func_Code_Type;
end record;
-- attempts to reflect packet binary layout
for Packet use
record at mod 1;
Station_Addr at 0 range 0 .. 7;
Func_Code at …Run Code Online (Sandbox Code Playgroud) ada ×1