Can I static cast a "parameter int" to a "parameter string"?

Is static casting of parameters allowed?

Let’s say I have a simple snippet of code:

parameter int NB = 7;
parameter string STR = string’(NB);

If I check the value of “STR”, that seems to be a blank string.
Simulators don’t complain about the code at any stage (compilation, elaboration or run-time).

Thanks

In reply to stf.cpll:

According to the 2017 LRM section 11.2.1 Constant expressions

11.2.1 Constant expressions
Some statement constructs require an expression to be a constant expression. The operands of a constant expression consist of constant numbers, strings, parameters, constant bit-selects and part-selects of parameters, constant function calls (see 13.4.3), and constant system function calls only. Constant expressions can use any of the operators defined in Table 11-1.Constant system function calls are calls to certain built-in system functions where the arguments meet conditions outlined in this subclause. When used in constant expressions, these function calls shall be evaluated at elaboration time. The system functions that may be used in constant system function calls are pure functions, i.e., those whose value depends only on their input arguments and that have no side effects."

Based on the above I’d say it is allowed to do such casting as this is not creating any side effect (I could be wrong)

Now what is the output you are expecting after converting the value 7 to a string? maybe what you are looking is for $sformatf(“%0d”, NB)
For example:


module test();
  parameter int NB      = 7;
  parameter string STR  = string'(NB);
  parameter string STR2 = $sformatf("%0d", NB);
  
  
  initial begin
    $display("NB   = %0d", NB);
    $display("STR  = %s", STR);// ascii code for 7 is "bell"
    $display("STR2 = %s", STR2);
  end
endmodule


which outputs

# KERNEL: NB   = 7
# KERNEL: STR  = a
# KERNEL: STR2 = 7

Also look at the https://www.asciitable.com/ so that you can understand what is output you are getting
HTH,

In reply to rgarcia07:

Hi,

thank you very much for your answer. I also ended up using the $sformatf function, but I would have expected the static cast to work, too. It seems that in your case, it is also a blank (STR). $sformatf seems to be returning the desired output.

Thank you.

In reply to stf.cpll:

A string is an ordered set of 8-bit characters, each character represents an 8-bit ASCII code.

Static casting a 32-bit int to a string converts 4 8-bit values to 4 ASCII characters. Nulls or 0 characters are removed.

The ASCII code 07 is the invisible character BEL (rings the bell).
The ASCII code for the character “7” is 55 ('h37)