gpt4 book ai didi

c - verilog 中的 ascii-hex 转换

转载 作者:行者123 更新时间:2023-11-30 16:03:26 33 4
gpt4 key购买 nike

我正在寻找一个 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 代码片段访问?

或者请告诉我 Verilog 本身是否有更简单的方法?

最佳答案

要输出到文本文件,您可以执行以下操作:

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", 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74

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

关于c - verilog 中的 ascii-hex 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4131160/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com