On Sep 28, 2:59*pm, hssig <hs...@gmx.net> wrote:
> How can I convert test_num (ranging from 0001 to 9999) to a string
> correctly taking into account the leading zeros?
Don't forget that the integer doesn't know about leading
zeros. You are entitled to write them in the integer literal,
but they are of course NOT stored in the integer itself. So
you need a format mechanism:
function format(
value : natural; --- the numeric value
width : positive; -- number of characters
leading : character := ' ')
return string --- guarantees to return "width" chars
is
constant img: string := integer'image(value);
variable str: string(1 to width) := (others => leading);
begin
if img'length > width then
report "Format width " & integer'image(width)
& " is too narrow for value " & img
severity warning;
str := (others => '*');
else
str(width+1-img'length to width) := img;
end if;
return str;
end;
....
---- this line should give "0055"
constant N: integer := 55;
report "value is " & format(integer'image(N), 4, '0');
Any use?
Dealing with negative integers is left as an exercise

--
Jonathan Bromley