我需要将整数转换为3个字符长的字符串Hexa小数,这样对于10我需要得到00A。我不确定这样做的最有效方法是什么。这就是我目前所拥有的,但是下面的方法在输出中添加了16#,增加了字符串的长度。
function Integer2Hexa(Hex_Int : Integer) return String is
Hexa : String(1..3);
begin
Ada.Integer_Text_IO.Put(Hexa,Hex_Int,16);
return Hexa;
end Integer2Hexa;
提前致谢。
答案 0 :(得分:3)
这是使用Ada标准库的实现。这很简单,但可能效率低下。
with Ada.Integer_Text_IO;
with Ada.Text_Io;
with Ada.Strings.Fixed;
procedure Dec2Hex is
function Integer2Hexa (Hex_Int : Integer; Width : Positive := 3)
return String is
Hex_Prefix_Length : constant := 3;
Hexa : String (1 .. Hex_Prefix_Length + Width + 1);
Result : String (1 .. Width);
Start : Natural;
begin
Ada.Integer_Text_IO.Put (Hexa,Hex_Int, 16);
Start := Ada.Strings.Fixed.Index (Source => Hexa, Pattern => "#");
Ada.Strings.Fixed.Move
(Source => Hexa (Start + 1 .. Hexa'Last - 1),
Target => Result,
Justify => Ada.Strings.Right,
Pad => '0');
return Result;
end Integer2Hexa;
begin
Ada.Text_Io.Put_Line (Integer2Hexa (10));
Ada.Text_Io.Put_Line (Integer2Hexa (16#FFF#));
Ada.Text_Io.Put_Line (Integer2Hexa (6));
Ada.Text_Io.Put_Line (Integer2Hexa (32, Width => 4));
end Dec2Hex;
答案 1 :(得分:2)
我也不确定最有效的方法,但每当我遇到使用除10之外的其他基础将整数转换为字符串表示的问题时,我会考虑使用Dmitry A. Kazakovs Simple Components and in特别是它的Strings_Edit包: http://www.dmitry-kazakov.de/ada/strings_edit.htm