我编写了一个代码来读取文本并将相同的文本写入另一个文本文件,我想将从输入文本中读取的每个字符转换为相应的二进制值。阅读和书写文本完美有效,但问题是它只显示与文本中第一个字符对应的值。请告诉我我在做错的地方。这是我的代码:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use std.textio.all;
use IEEE.NUMERIC_STD.ALL;
entity asconvbin is
Port ( clk : in STD_LOGIC;
d : out STD_LOGIC_VECTOR(7 downto 0));
end asconvbin;
architecture Behavioral of asconvbin is
begin
process(clk)
variable OUTLINE : LINE;
file FILEOUT : TEXT is OUT "outputfile.txt";
--Input variables
variable inline:line;
variable char:character;
variable end_of_line:boolean;
file myfile:text is "myfile.txt";
variable k:integer;
begin
if rising_edge(clk) then
while not endfile(myfile) loop
readline(myfile,inline);
k:=inline'high;
end_of_line := true;
while end_of_line loop
read(inline,char,end_of_line);
for i in k downto 0 loop
if char='A' then d<="01000000";
elsif char='B' then d<="01000001";
elsif char='C' then d<="01000010";
elsif char='D' then d<="01000011";
elsif char='E' then d<="01000100";
elsif char='F' then d<="01000101";
elsif char='G' then d<="01000110";
elsif char='H' then d<="01000111";
elsif char='I' then d<="01001000";
elsif char='J' then d<="01001001";
elsif char='K' then d<="01001010";
elsif char='L' then d<="01001011";
elsif char='M' then d<="01001100";
elsif char='N' then d<="01001101";
elsif char='O' then d<="01001110";
elsif char='P' then d<="01001111";
elsif char='Q' then d<="01010000";
elsif char='R' then d<="01010001";
elsif char='S' then d<="01010010";
elsif char='T' then d<="01010011";
elsif char='U' then d<="01010100";
elsif char='V' then d<="01010101";
elsif char='W' then d<="01010110";
elsif char='X' then d<="01010111";
elsif char='Y' then d<="01011000";
elsif char='Z' then d<="01011001";
else null;
end if;
k:=k-1;
end loop;
if end_of_line then
WRITE(OUTLINE,char);
end if;
end loop;
end loop;
WRITELINE(FILEOUT, OUTLINE);
end if;
end process;
end Behavioral;
答案 0 :(得分:0)
d
仅输出第一个字符值的原因是k
在for i in k downto 0 loop
中使用k := k-1
递减,从而产生
在k
中,输入文件中的第二个字符为-1,然后是范围
for i in -1 downto 0 loop
,这是一个空范围,从而导致循环
被跳过,因此没有后续分配给d
。代码熟了:
while not endfile(myfile) loop
readline(myfile, inline);
k := inline'high;
...
while end_of_line loop
...
for i in k downto 0 loop
if char = 'A' then d <= "01000000";
...
end if;
k := k-1;
end loop;
...
end loop;
end loop;
将char
转换为std_logic_vector
的另一种方法是:
d <= std_logic_vector(to_unsigned(character'pos(char), d'length));