【问题标题】:How to convert a string to integer in VHDL?如何在VHDL中将字符串转换为整数?
【发布时间】:2011-09-01 13:35:19
【问题描述】:

我正在将文本数据加载到 VHDL 测试台中,并且我想将输入字符串转换为整数值。

例如:“123” => 123

有人可以推荐一种在 VHDL 中将字符串转换为整数的“最佳”方法吗?

【问题讨论】:

    标签: string integer type-conversion vhdl


    【解决方案1】:

    仅供参考。也可以使用'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()
    

    【讨论】:

    • +1,但值得注意的是,综合工具对这两个选项都没有很好的支持。 Vivado 2020.1 无法综合任一选项。对于可合成的代码,我只写了一个小解析器,它使用character'pos(x(i)) - character'pos('0') 转换每个数字。不要忘记减号。
    【解决方案2】:

    readlineread 函数应该可以实现您所寻找的。​​p>

    基本上:

    1. 打开您的文件
    2. 使用 readline 从文件中获取下一行到行缓冲区中
    3. 使用 read 将行缓冲区解析为有用数据
    4. (可选)根据需要转换解析值

    代码片段:

    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
    ...
    

    【讨论】:

    • 那位先生是一个很好的答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多