integer'image string width

N

Nicolas Matringe

Guest
Hello all
Is there a way to force the character string to a fixed length when
using integer'image attribute?
I want to generate file names using a counter and I need a fixed string
length. Is it possible to add a parameter, such as
integer'image(int_number, 3) ?

Thanks
Nicolas
 
Nicolas Matringe a écrit :

Hello all
Is there a way to force the character string to a fixed length when
using integer'image attribute?
I want to generate file names using a counter and I need a fixed string
length. Is it possible to add a parameter, such as
integer'image(int_number, 3) ?
No, you can't do that with the 'image attribute.

Write your own function. You may use std.textio.write

JD.
 
John Doef a écrit :
Nicolas Matringe a écrit :
Is there a way to force the character string to a fixed length when
using integer'image attribute?
Is it possible to add a parameter, such as integer'image(int_number, 3) ?

No, you can't do that with the 'image attribute.
I worked around but it's a bit clumsy (handle 1, 2 or 3 digits cases
separately and pad the string accordingly).

Nicolas
 
Nicolas Matringe wrote:

Is there a way to force the character string to a fixed length when
using integer'image attribute?
See the function fix below

-- Mike Treseler

----------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use std.textio.all;
entity fix_str is
end entity fix_str;
-- Thu Apr 13 09:39:48 2006 M. Treseler
architecture sim of fix_str is
begin
what:process is
----------------------------------------------
function fix
( arg_str : string;
ret_len_c : natural := 10;
fill_char_c : character := '0' )
return string is
variable ret_v : string (1 to ret_len_c);
constant pad_len_c : integer := ret_len_c - arg_str'length ;
variable pad_v : string (1 to abs(pad_len_c));
begin
if pad_len_c < 1 then
ret_v := arg_str(ret_v'range);
else
pad_v := (others => fill_char_c);
ret_v := pad_v & arg_str;
end if;
return ret_v;
end fix;
----------------------------------------------
procedure print
(str1 : in string := "") is
constant newline_c : string := (1 => LF);
constant line_c : string := newline_c & str1;
begin
write(std.textio.output, line_c);
end procedure print;
----------------------------------------------
begin
print( fix("1"));
print( fix("123"));
print( fix("1234567890"));
print( fix("1234567890123"));
print( fix(integer'image(98765)));
print( fix(integer'image(98765),8));
print( fix(integer'image(98765),8,'_'));
wait;
end process what;
end architecture sim;
----------------------------------------------
--vsim -c fix_str
--VSIM 1> run
--# 0000000001
--# 0000000123
--# 1234567890
--# 1234567890
--# 0000098765
--# 00098765
--# ___98765
--VSIM 2>
 
Mike Treseler a écrit :
Nicolas Matringe wrote:

Is there a way to force the character string to a fixed length when
using integer'image attribute?

See the function fix below
Thanks a lot Mike. I thought you would have a solution :eek:)

Nicolas
 
Nicolas Matringe wrote:

Thanks a lot Mike. I thought you would have a solution :eek:)
You're welcome. Interesting question.
Here's the same function with one less variable.

-- Mike Treseler


function fix
(arg_str : string;
ret_len_c : natural := 10;
fill_char_c : character := '0')
return string is
variable ret_v : string (1 to ret_len_c);
constant pad_len_c : integer := ret_len_c - arg_str'length;
constant pad_c : string (1 to abs(pad_len_c))
:= (others => fill_char_c);
begin
if pad_len_c < 1 then
ret_v := arg_str(ret_v'range);
else
ret_v := pad_c & arg_str;
end if;
return ret_v;
end fix;
 

Welcome to EDABoard.com

Sponsor

Back
Top