【问题标题】:ascii-hex conversion in verilogVerilog中的ascii-hex转换
【发布时间】:2010-11-09 06:50:56
【问题描述】:

我正在寻找一个 Verilog 函数来将我的 ASCII 输入字符串转换为十六进制输出。我不确定我是否可以在 C 中做到这一点并与 Verilog 一起使用。到目前为止,我能够使用以下命令将输入​​的 ASCII 字符串打印为十六进制值:

printf("Hexadecimal Output for <CALL-ID>: ");
for(c = 0; c < strlen(callid); c++)
{
  printf("TO: %x ", callid[c]);
}

有没有办法将输出保存在文本/csv 文件中,并使其可供我的 verilog 代码 sn-p 访问?

或者请让我知道 Verilog 本身是否有更简单的方法?

【问题讨论】:

  • 完全不相关,但您应该避免在每次循环迭代时调用strlen()strlen() 不是 O(1) 操作。你应该改为size_t max = strlen(callid); for(c = 0; c &lt; max; c++)
  • @Chris:您的建议有帮助,但仍然很愚蠢。而是将循环条件从 c&lt;strlen(callid) 更改为简单的 callid[c] 并完全避免调用 strlen
  • 您是否尝试使用 ASCII 字符驱动 verilog 总线?

标签: c ascii hex verilog


【解决方案1】:

要输出到文本文件,你可以这样做:

static int string_to_hex_file(const char *filename, const char *string)
{
    FILE            *out;
    const char      *s;

    if((out = fopen(filename, "w")) == NULL)
      return 0;

    fprintf(out, "\"%s\", ", string);
    for(s = string; *s; s++)
    {
        if(s != string)
            fprintf(out, ", ");
        fprintf(out, "0x%02x", (unsigned char) *s);
    }
    fprintf(out, "\n");

    fclose(out);
    return 1;
}

string == "this is a test" 的示例输出:

"this is a test", 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74

当然可以改进错误检查,您可能需要调整输出格式以满足您的需要。

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 2019-09-13
    • 2012-11-02
    • 2015-11-06
    • 2014-08-25
    • 2012-03-01
    • 2015-10-30
    • 1970-01-01
    相关资源
    最近更新 更多