Liam P
Liam P

Reputation: 217

Store multiple variables as a single hex string

I am looking for a way to store a few variables in one variable so I can output a string. For example, I have variables:

int flow_val = "128";
int numb_val = "104";
int size_val = "256";

I can put them together using $display like this:

$display("32'h%0h%0h_%4h", flow_val, numb_val, size_val);

to print out:

32'h8068_0100

Is there a way to get that output and put it into a variable such as hex_val?

Upvotes: 1

Views: 611

Answers (1)

toolic
toolic

Reputation: 62236

$sformatf can be used. Refer to IEEE Std 1800-2012, section 21.3.3 Formatting data to a string.

module tb;

int flow_val = 128;
int numb_val = 104;
int size_val = 256;
string hex_val;

initial begin
    hex_val = $sformatf("32'h%0h%0h_%4h", flow_val, numb_val, size_val);
    $display(hex_val);
end

endmodule

Output:

32'h8068_0100

Upvotes: 2

Related Questions