GNAT Get(FRom => SomeString, item => SomeInteger, last => last)中的错误?

Bug in GNAT Get(FRom => SomeString, Item => SomeInteger, Last => Last)?

提问人:Erlo 提问时间:5/24/2023 更新时间:5/24/2023 访问量:73

问:

Get(TheFile, IntValue);非常适合格式化为 16#12# 的字符串来读取十六进制值。 不应该以同样的方式从字符串函数中获取吗?

我试过了,但通过 16#12# 只能得到 16。纯十六进制,例如 F8 导致异常

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure hex is
   IntValue  : Integer;
   Last : Positive;
begin
   Put("Enter a hexadecimal string: ");
   Get(HexString);

   -- Convert the hexadecimal string to an integer
   Get(From => HexString, Item => IntValue, Last => Last);

   Put ("The integer value is: ");
   Put (IntValue, Width => 0);
   New_Line;
end hex;

字符串 输入 ada gnat

评论


答:

2赞 Jim Rogers 5/24/2023 #1

《Ada 语言参考手册》指出:

procedure Get(From : in String; Item : out Num; Last : out Positive);

从给定字符串的开头读取整数值, 遵循与读取整数的 Get 过程相同的规则 文件中的值,但将字符串的末尾视为文件 终结者。在参数 Item 中返回 Num 类型的值,该值 对应于序列输入。在 Last 中返回索引值 这样,From(Last) 是读取的最后一个字符。

如果序列输入未传播异常Data_Error 具有所需的语法,或者如果获得的值不是 子类型 Num。

从文件中读取值的解释是

procedure Get(File : in File_Type; Item : out Num; Width : in Field := 0);
procedure Get(Item : out Num; Width : in Field := 0);

如果参数 Width 的值为零,则跳过任何前导空格, 行终止符或页面终止符,然后读取加号,如果 存在或(仅适用于有符号类型)减号(如果存在),则 读取与语法匹配的最长字符序列 没有点的数字文字。如果 Width 的非零值为 提供,则准确输入宽度字符,或字符 (可能没有)最多一个行终结器,以先到者为准;任何 跳过的前导空格包括在计数中。

get 函数工作正常。 如果要转换包含 Ada 十六进制值(如 16#12#)的字符串,则应使用 'Value 属性。

with Ada.Text_IO; use Ada.Text_IO;

procedure convert_hex is
   Num : Integer := Integer'Value ("16#12#");
begin
   Put_Line (Num'Image);
end convert_hex;

评论

0赞 Erlo 5/25/2023
根据 RM,Get from string 使用与 Get from file 相同的规则。但显然它没有,因为从文件获取会产生预期的结果?
2赞 Simon Wright 5/24/2023 #2

好吧,这不可能是你运行的代码,因为你还没有声明.另外,那是什么?HexStringGet

这可以按照您的意愿工作:

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure hex is
   IntValue  : Integer;
   HexString : String (1 .. 16);
   Last : Natural;
begin
   Put("Enter a hexadecimal string: ");
   Get_Line(HexString, Last => Last);

   -- Convert the hexadecimal string to an integer
   Get(From => HexString (1 .. Last), Item => IntValue, Last => Last);

   Put ("The integer value is: ");
   Put (IntValue, Width => 0);
   New_Line;
end hex;

评论

0赞 Erlo 5/25/2023
第一个 Get 只是读取一个字符串。是的,我忘了在示例中声明变量,但在我的代码中它就在那里。
0赞 Simon Wright 5/25/2023
这到底返回了什么字符串?我假设您的意思是 ARM A.10.7(14) 中的那个,其中读取的字符数由相关字符串的长度决定。注意,我们正在争论可能或可能不在您的机器上的代码。如果您编辑问题以显示您的实际代码,则解决问题的机会会大得多Get
1赞 Erlo 5/26/2023
我不知道我一开始尝试这个时做错了什么,但如果字符串格式正确,实际上可以工作 - 以 16#ff# 为例。很抱歉噪音,感谢您的输入。Get(From => HexString, Item => IntValue, Last => Last);