integer'image string width

N

Nicolas Matringe

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
 
J

john Doef

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.
 
N

Nicolas Matringe

John Doef a écrit :
Nicolas Matringe a écrit :
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
 
M

Mike Treseler

Nicolas said:
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>
 
M

Mike Treseler

Nicolas said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top