Integer with leading zeros to string

Discussion in 'VHDL' started by hssig, Sep 28, 2010.

  1. hssig

    hssig Guest

    Hi,

    I have the following integer declaration:

    signal test_num : integer := 0555;

    Now I want to make a string out of it:

    signal test_string : string (1 to 4);
    begin
    test_string <= integer'image(test_num);

    Modelsim complains "Array lengths do not match. Left is 4 (1 to 4).
    Right is 3 (1 to 3)."

    How can I convert test_num (ranging from 0001 to 9999) to a string
    correctly taking into account the leading zeros?

    Cheers,
    hssig
    hssig, Sep 28, 2010
    #1
    1. Advertising

  2. hssig

    hssig Guest

    The following should be displayed:


    report "This should be displayed " & integer'image(test_num) severity
    note;

    -> This should be displayed 0555

    cheers,
    hssig
    hssig, Sep 28, 2010
    #2
    1. Advertising

  3. On Sep 28, 2:59 pm, hssig <> 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
    Jonathan Bromley, Sep 28, 2010
    #3
  4. On Tue, 28 Sep 2010 09:36:39 -0700 (PDT), Jonathan Bromley wrote:

    >function format(
    > value : natural; --- the numeric value
    > width : positive; -- number of characters
    > leading : character := ' ')
    >return string --- guarantees to return "width" chars


    [...]

    >constant N: integer := 55;
    >report "value is " & format(integer'image(N), 4, '0');


    oops, obviously the first argument should
    be N rather than integer'image(N).
    Hasty end-of-working-day post.
    --
    Jonathan Bromley
    Jonathan Bromley, Sep 28, 2010
    #4
  5. On Tue, 28 Sep 2010 06:59:09 -0700 (PDT), hssig wrote:

    >How can I convert test_num (ranging from 0001 to 9999) to a string
    >correctly taking into account the leading zeros?


    One final afterthought: take a look at
    http://www.easics.com/webtools/freesics
    for a more general (and very cunning) solution.
    --
    Jonathan Bromley
    Jonathan Bromley, Sep 28, 2010
    #5
  6. hssig

    hssig Guest

    Hi Jonathan,

    I like you proposals. Thank you very much.

    Cheers,
    hssig
    hssig, Sep 29, 2010
    #6
  7. hssig

    Andy Guest

    On Sep 28, 11:36 am, Jonathan Bromley <>
    wrote:
    > 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:
    > --
    > Jonathan Bromley


    VHDL integers do store leading zeroes, up to at least 32 binary bits
    total. However, as you stated, standard output formats do not display
    them. Just because you initialize an integer with a literal which
    happens to be formatted with leading zeroes does not mean that the
    display output from that same integer will automatically be formatted
    with leading zeroes.

    Andy
    Andy, Sep 29, 2010
    #7
  8. On Sep 29, 3:44 pm, Andy <> wrote:

    > VHDL integers do store leading zeroes, up to at least 32 binary bits
    > total. However, as you stated, standard output formats do not display
    > them. Just because you initialize an integer with a literal which
    > happens to be formatted with leading zeroes does not mean that the
    > display output from that same integer will automatically be formatted
    > with leading zeroes.


    yeah, guess I didn't say very clearly what I meant :)

    let's try again: there is no information stored in the
    integer variable that indicates whether or not its
    original textual representation had leading zeros (and,
    indeed, no information about any other aspect of its
    original textual representation except the integer's
    numeric value).

    Ho hum....

    Jonathan
    Jonathan Bromley, Sep 29, 2010
    #8
    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. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    7,968
    Oliver Sturm
    Oct 13, 2005
  2. Luke Zhang [MSFT]

    Re: CSV for Excel - Problem with Leading Zeros

    Luke Zhang [MSFT], Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,234
    Luke Zhang [MSFT]
    Jun 26, 2003
  3. GIMME
    Replies:
    5
    Views:
    43,679
    Dale King
    Feb 13, 2004
  4. r.magdeburg

    leading zeros - wrong int

    r.magdeburg, Dec 1, 2003, in forum: C++
    Replies:
    2
    Views:
    1,720
    red floyd
    Dec 1, 2003
  5. Stian
    Replies:
    2
    Views:
    582
    Thomas D'Tak
    Sep 19, 2004
Loading...

Share This Page