我正在将文本数据加载到VHDL测试台中,我想将输入字符串转换为整数值。
例如:"123" => 123
有人能推荐一种在VHDL中将字符串转换为整数的“最佳”方法吗?
我正在将文本数据加载到VHDL测试台中,我想将输入字符串转换为整数值。
例如:"123" => 123
有人能推荐一种在VHDL中将字符串转换为整数的“最佳”方法吗?
为了参考。也可以使用'value
属性将字符串转换为整数:
variable str : string := "1234";
variable int : integer;
...
int := integer'value(str);
根据需求,这可能比read()
过程更可取,因为它不会破坏源字符串。然而,它只能在字符串是一个有效的整数文字,且没有空格之外的周围字符时工作。
variable ln : line;
variable int : integer;
...
ln := new string'(" 456 "); -- Whitespace will be ignored
int := integer'value(ln.all); -- Doesn't consume contents of ln
ln := new string'("789_000 more text");
int := integer'value(ln.all); -- This will fail unlike read()
readline和read函数应该可以实现您正在寻找的功能。
基本上:
代码片段:
library STD;
use std.textio.all;
...
variable File_Name : string;
file my_file : text;
variable lineptr : line;
variable temp : integer;
...
file_open(my_file, File_Name, read_mode); -- open the file
readline(my_file, lineptr); -- put the next line of the file into a buffer
read(lineptr, temp); -- "parse" the line buffer to an integer
-- temp now contains the integer from the line in the file
...
character'pos(x(i)) - character'pos('0')
将每个数字转换。别忘了减号。 - Harry