关于verilog中的c:ascii-hex转换 | 珊瑚贝

ascii-hex conversion in verilog


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

1
2
3
4
5
printf(“Hexadecimal Output for <CALL-ID>:”);
for(c = 0; c < strlen(callid); c++)
{
  printf(“TO: %x”, callid[c]);
}

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

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

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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,\
);

    fclose(out);
    return 1;
}

string ==”this is a test”:

的示例输出

1
“this is a test”, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74

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


来源:https://www.codenong.com/4131160/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9390.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?