What is the 'standard' way to convert an std_logic_vector value into character type? It seems that first I convert it to an integer and then to character using the ASCII table. However, I am not sure how to do this in code.
Also, what is the 'standard' way to do the reverse conversion from character to std_logic_vector?
Some answers on the internet are quite old. This question is about the current form of the VHDL language and its libraries.
1 Answer 1
In terms of standard, character
is an enumerated type which maps to ISO-8859-1 (i.e. ASCII for codes 0-127).
There are standard predefined attribute functions on enumerated types that can convert between enumerated value and its position in enumeration ('pos()
and 'val()
), then, character conversion to its 8-bit encoding is simple enough:
subtype byte is std_ulogic_vector(7 downto 0);
function to_byte(c : character) return byte is
begin
return byte(to_unsigned(character'pos(c), 8));
end function;
function to_character(b: byte) return character is
begin
return character'val(to_integer(unsigned(b)));
end function;
-
\$\begingroup\$ So character is not defined for values between 128 and 255? \$\endgroup\$gyuunyuu– gyuunyuu2022年07月05日 10:19:15 +00:00Commented Jul 5, 2022 at 10:19
-
1\$\begingroup\$ Character is, ISO-8859-1 is, ASCII is not. \$\endgroup\$Nipo– Nipo2022年07月05日 10:19:56 +00:00Commented Jul 5, 2022 at 10:19
function to_string (inp: std_logic_vector) return string is variable image_str: string (1 to inp'length); alias input_str: std_logic_vector (1 to inp'length) is inp; begin for i in input_str'range loop image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i))); end loop; return image_str; end function;
\$\endgroup\$