如何在vhdl中使用前导零将整数转换为字符串?

时间:2017-03-02 13:48:57

标签: vhdl

如何在VHDL中打印带前导零的整数?

实质上与C或Python相同:

printf("%05d", number);
str(number).zfill(5)

当然,这仅适用于模拟(写入文件或报告等)。

6 个答案:

答案 0 :(得分:2)

到目前为止,我提出的最好的方法就是这个功能:

-- Range is limited to from 1 to 9 as 10 digit
-- integer can already overflow in VHDL
function fIntToStringLeading0 (a : natural; d : integer range 1 to 9) return string is
  variable vString : string(1 to d);
begin
  if(a >= 10**d) then
    return integer'image(a);
  else
    for i in 0 to d-1 loop
      vString(d-i to d-i) := integer'image(a/(10**i) mod 10);
    end loop;
    return vString;
  end if;
end function;

例如:

  process  is
    variable number : integer := 42;
  begin
    report fIntToStringLeading0(number, 5);
    wait;
  end process;

输出:

  

#**注:00042

答案 1 :(得分:2)

有一个针对C风格格式的开源软件包。您可以在https://github.com/suoto/hdl_string_format

找到它

答案 2 :(得分:2)

有VHDL方式。编写一个普遍适用的to_dstring函数:

rightjustd.vhdl:38:9:@0ms:(report note): -00000002456
rightjustd.vhdl:39:9:@0ms:(report note): ####
rightjustd.vhdl:40:9:@0ms:(report note): -2456
rightjustd.vhdl:41:9:@0ms:(report note): -2456       .
rightjustd.vhdl:42:9:@0ms:(report note):        -2456

这还没有经过彻底测试。测试用例产生:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website.net [NC]
RewriteRule ^(.*)$ https://www.website.net/$1 [L,R=301,NC]
rewriteRule ^market[/]?$ /Marketplace/index.php [L]
RewriteRule ^profile/([A-Z,a-z,0-9]+)[/]?$ Profile.php?id=$1 [NC,L]
RewriteRule ^market/Item([A-Z,a-z,0-9]+)[/]?$ /Marketplace/item.php?id=$1     [NC,L]
RewriteRule ^pics/([A-Z,a-z,0-9]+)[/]?$ /my/pics/picture.php?id=$1 [NC,L]

(类常量)输入的初始值意味着您可以从右到左一直关闭参数,只提供一个整数值。

对齐函数调用需要-2008并在包textio中声明。

答案 3 :(得分:1)

这里有另一个想法,即除去检查以确保使用TEXTIO写入过程需要任何前导零,这样做会进行这种检查。麻烦的是,写过程放入前导空格而不是零,用零替换空格是笨重的:

for tr in train_data:
    new.write(tr.strip() + ' , '  + str(index))

例如

  function fIntToStringLeading0 (a : natural; d : integer range 1 to 9) return string is
    variable L : line;
  begin
    write(L,a,right,d);
    for I in 1 to L.all'length loop
      if L.all(I) = ' ' then
        L.all(I) := '0';
      end if;
    end loop;
    return L.all;
  end function;

https://www.edaplayground.com/x/39di

答案 4 :(得分:0)

这是一个双线:

  ZEROES := (others => '0');
  ZEROES(ZEROES'length-integer'image(N)'length+1 to ZEROES'length) := integer'image(N);

例如

entity LEADING_ZEROES is
end entity ;

architecture LEADING_ZEROES of LEADING_ZEROES is
begin

  process
    constant MAXLENGTH : integer := 10;
    variable N : integer;
    variable ZEROES : string(1 to MAXLENGTH);
  begin
    N := 1234;
    ZEROES := (others => '0');
    ZEROES(ZEROES'length-integer'image(N)'length+1 to ZEROES'length) := integer'image(N);
    report ZEROES;
    wait;
  end process;

end architecture LEADING_ZEROES;

https://www.edaplayground.com/x/4B84

将它包装在像你这样的函数中可能更好,并添加你的检查:

  function fIntToStringLeading0 (a : natural; d : integer range 1 to 9) return string is
    variable vString : string(1 to d) := (others => '0');
  begin
    if(a >= 10**d) then
      return integer'image(a);
    else
      return vString(1 to vString'length-integer'image(a)'length) & integer'image(a);
    end if;
  end function;

例如

entity LEADING_ZEROES is
end entity ;

architecture LEADING_ZEROES of LEADING_ZEROES is
  function fIntToStringLeading0 (a : natural; d : integer range 1 to 9) return string is
    variable vString : string(1 to d) := (others => '0');
  begin
    if(a >= 10**d) then
      return integer'image(a);
    else
      return vString(1 to vString'length-integer'image(a)'length) & integer'image(a);
    end if;
  end function;
begin

  process
  begin
    report fIntToStringLeading0(1234,5);
    report fIntToStringLeading0(12345,6);
    report fIntToStringLeading0(12345,3);
    wait;
  end process;

end architecture LEADING_ZEROES;

https://www.edaplayground.com/x/255x

答案 5 :(得分:0)

如果我正确理解您的意思,您只是想

printf("%05d", number);  // in C

进入

print("%05d" % number)   # in python
相关问题