integer'image string width

Discussion in 'VHDL' started by Nicolas Matringe, Apr 12, 2006.

  1. 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, Apr 12, 2006
    #1
    1. Advertising

  2. Nicolas Matringe

    john Doef Guest

    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, Apr 13, 2006
    #2
    1. Advertising

  3. 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, Apr 13, 2006
    #3
  4. 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, Apr 13, 2006
    #4
  5. 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, Apr 13, 2006
    #5
  6. 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;
    Mike Treseler, Apr 17, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndrewF
    Replies:
    1
    Views:
    765
    Bruce Barker
    Oct 10, 2005
  2. Xu
    Replies:
    11
    Views:
    8,551
    Andrew Thompson
    Jul 16, 2005
  3. Sean Jorden
    Replies:
    1
    Views:
    592
    laurie
    Aug 19, 2003
  4. Replies:
    1
    Views:
    737
    Beauregard T. Shagnasty
    Dec 29, 2007
  5. Randy Kramer
    Replies:
    12
    Views:
    370
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page