gpt4 book ai didi

VHDL整数到字符串

转载 作者:行者123 更新时间:2023-12-02 08:22:23 24 4
gpt4 key购买 nike

假设您想将整数转换为 VHDL 中的字符串,以便在 VGA 显示器上显示。你不能使用 ieee 2008 标准,因为你必须使用 xilinx ISE 14.7。我有以下用于将整数类型转换为字符串类型的代码,但在 while 循环和 for 循环中出现“超出非静态循环限制”错误:

-- convert integer to string using specified base
-- (adapted from Steve Vogwell's posting in comp.lang.vhdl)

function str(int: integer; base: integer) return string is

variable temp: string(1 to 10);
variable num: integer;
variable abs_int: integer;
variable len: integer := 1;
variable power: integer := 1;

begin

-- bug fix for negative numbers
abs_int := abs(int);

num := abs_int;

while num >= base loop -- Determine how many
len := len + 1; -- characters required
num := num / base; -- to represent the
end loop ; -- number.

for i in len downto 1 loop -- Convert the number to
temp(i) := chr(abs_int/power mod base); -- a string starting
power := power * base; -- with the right hand
end loop ; -- side.

-- return result and add sign if required
if int < 0 then
return '-'& temp(1 to len);
else
return temp(1 to len);
end if;

end str;

我通过将其变形为这种怪物来“解决”错误:

-- convert integer to string using specified base
-- (adapted from Steve Vogwell's posting in comp.lang.vhdl)

function str(int: integer; base: integer) return string is

variable temp: string(1 to 9);
variable num: integer;
variable abs_int: integer;
variable len: integer := 1;
variable power: integer := 1;

begin

-- bug fix for negative numbers
abs_int := abs(int);

num := abs_int;

for i in 0 to 100 loop
if (num >= base) then -- Determine how many
len := len + 1; -- characters required
num := num / base; -- to represent the
else -- number.
exit;
end if;
end loop ;

for i in 9 downto 1 loop -- Convert the number to
if (i <= len) then
temp(i) := chr(abs_int/power mod base); -- a string starting
power := power * base; -- with the right hand
else
exit;
end if;
end loop ; -- side.

-- return result and add sign if required
if int < 0 then
return '-'& temp(1 to len);
else
return temp(1 to len);
end if;

end str;

有没有一种非延迟的方法可以将整数转换为字符串?

最佳答案

如果 I 是一个整数,integer'image(I) 就是它作为字符串的表示。

关于VHDL整数到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com