【发布时间】:2011-09-01 13:35:19
【问题描述】:
我正在将文本数据加载到 VHDL 测试台中,并且我想将输入字符串转换为整数值。
例如:“123” => 123
有人可以推荐一种在 VHDL 中将字符串转换为整数的“最佳”方法吗?
【问题讨论】:
标签: string integer type-conversion vhdl
我正在将文本数据加载到 VHDL 测试台中,并且我想将输入字符串转换为整数值。
例如:“123” => 123
有人可以推荐一种在 VHDL 中将字符串转换为整数的“最佳”方法吗?
【问题讨论】:
标签: string integer type-conversion 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()
【讨论】:
character'pos(x(i)) - character'pos('0') 转换每个数字。不要忘记减号。
readline 和 read 函数应该可以实现您所寻找的。p>
基本上:
代码片段:
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
...
【讨论】: