How to write an integer to stdout as hexadecimal in VHDL?

Ciro Santilli 郝海东冠状病六四事件法轮功 picture Ciro Santilli 郝海东冠状病六四事件法轮功 · Jun 17, 2016 · Viewed 12.7k times · Source

I can print an integer as decimal to stdout with:

library std;
use std.textio.all;

entity min is
end min;

architecture behav of min is
begin
    process is
        variable my_line : line;
    begin
        write(my_line, 16);
        writeline(output, my_line);
        wait;
    end process;
end behav;

which outputs:

16

But how to output instead either:

10
0x10

Answer

scary_jeff picture scary_jeff · Jun 17, 2016

Assuming an integer i, and VHDL-2008, you could use:

write(output, integer'image(i) & LF);  -- Plain integer value
write(output, "0x" & to_hstring(to_signed(i, 32)) & LF);  -- Hexadecimal representation

You need to have use std.textio.all; for this to work. Change the 32 to reduce the length of the hex value. I chose 32 so that it can represent any integer value in most simulators.

These will also work for report statements, e.g.

report "i = 0x" & to_hstring(to_signed(i, 32));