2010-11-09 739 views
0

我正在尋找一個Verilog函數來將我的ASCII輸入字符串轉換爲十六進制輸出。我不確定我是否可以在C中使用Verilog來完成它。到目前爲止,我可以用打印的輸入ASCII字符串爲十六進制值:verilog中ascii-hex轉換

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

有沒有辦法來保存輸出文本/ CSV文件,並使其對我的Verilog代碼片段訪問?

或者請讓我知道在Verilog中是否有更簡單的方法?

+1

完全不相關,但是您應該避免每次循環迭代時調用'strlen()'。 'strlen()'不是O(1)操作。你應該改爲'size_t max = strlen(callid); for(c = 0; c 2010-11-09 06:53:34

+1

@Chris:你的建議有幫助,但仍然很愚蠢。相反,將環路條件從'c 2010-11-09 07:25:09

+1

您是否試圖用ASCII字符驅動verilog總線? – Marty 2010-11-09 16:01:41

回答

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 

錯誤檢查當然可以改善,那麼您可能需要調整輸出格式以適應您的需求。