从文件读取输入'Size
并'Component_Size
尝试使用Unchecked_Conversion
. 我知道要成功使用Unchecked_Conversion
Source 和 Target 需要相同size
。我正在从文件中读取输入,000100000101001
并希望使用 Unchecked Conversion 将其放入位数组中。但是,转换似乎总是失败,因为它们的大小不同或太小。
with Ada.Unchecked_Conversion;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure main is
type Bit is mod 2 with size => 1;
type Bit_Array is array(Positive range <>) of Bit with pack;
subtype Bit15 is Bit_Array(1 .. 15); //subtypes because can't use conversion on unconstrainted type
subtype Bit11 is Bit_Array(1 .. 11);
function StringA_to_Bit15 is
new Ada.Unchecked_Conversion(source => String, target => Bit15);
begin
while not end_of_file loop
declare
s: String := get_line; //holding first line of input
len: constant Natural := (if s'length-3 = 15
then 15
else 11);
i: Integer;
ba: Bit_Array(1 .. len); //constrain a Bit_Array based on length of input
begin
ba := String_to_Bit15(s);
new_line;
end;
end loop;
这是我的类型,位只能是 0 或 1size
到 1 位。Bit_Array 只是一个不受约束的位数组,因为我的输入可以是 15 位长或 11 位长。我的想法是将第一行读入字符串并将其转换为 Bit_Array。这不起作用,因为 String 和其他所有原始类型都不是Size => 1
. 所以很自然地,我想创建一个新类型来处理这个我尝试过的形式,创建我自己的字符串类型并设置size => 1
但字符需要 8 位。我需要创建什么数据类型来读取一行数据并将其转换为适合 Bit_Array?我可能正在接近这个错误,但它对我来说非常混乱。任何帮助或提示表示赞赏!