Converting logic_vector -> natural

M

magik

Hi

I want to convert vect(3 downto 0) into natural digit

is there any standard conversion procedure/function in vhdl for that??

Paul
 
D

David Bishop

magik said:
Hi

I want to convert vect(3 downto 0) into natural digit

is there any standard conversion procedure/function in vhdl for that??

use ieee.numeric_std.all;

.....
variable natvar : natural;
variable vect : std_logic_vector (3 downto 0);
......

natvar := to_integer(unsigned(vect));
 
R

Ralf Hildebrandt

magik said:
I used conv_integer from std_logic_unsigned and the result is the same :)

Don't do it. First this library is /not/ a standard library. Your code is not protable.
Second you always assume /unsigned/ numbers in the vector. It is much better to treat
std_(u)logic_vectors neighter as signed nor as unsigned number in general and to choose it
using the conversion unsigned() or signed() dependend on the context.

O.k. - for your conversion to natural, which cover non-negative integers there is no
problem, but a conversion to integer would never lead to negative numbers.

Ralf
 
Joined
May 23, 2011
Messages
2
Reaction score
0
Logic_vector to Natural

--CONSTANT declaration
constant COUNTER_LOGIC : integer := 8;

signal count : std_logic_vector (COUNTER_LOGIC-1 downto 0);


-- Felt it very useful if you want to avoid writing code multiple times and then thinking of scaling logic in the long run
start_of_frameopti_reg(conv_integer(count)) <= '1';
start_of_frameopti_reg((conv_integer(count))-1) <= '0';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top