Extracting digits [0-9] from an number/integer

M

--mms

Guest
Hi:


I would like to know if there is any function/method, in VHDL, to
extract the digits of an integer.

What I wish to do is take, for example, an integer like 2043, and
extract first the number '3', then '4', '0', and '2'.

Each digit will then be converted to a std_logic_vector(3 downto 0).



Thank you!,
m m s
 
On Sat, 12 Jul 2008 21:31:06 -0700 (PDT), --mms <msmeerkat@gmail.com>
wrote:

Hi:


I would like to know if there is any function/method, in VHDL, to
extract the digits of an integer.

What I wish to do is take, for example, an integer like 2043, and
extract first the number '3', then '4', '0', and '2'.

Each digit will then be converted to a std_logic_vector(3 downto 0).
So you're trying to convert decimal to BCD ("binary coded decimal").

There's a neat shift-and-subtract trick that has been mentioned here
many times before, and maps easily to hardware - google for
"binary to BCD fpga" should do it.

How is your integer represented? Is it a binary number (an
integer signal or variable, or a std_logic_vector or numeric_std
signal or variable?

A non-synthesisable version might look like this - note it will do
hex, binary and octal conversion as well...

subtype BCD_digit is std_logic_vector(3 downto 0);
type BCD_number is array (natural range <>) of BCD_digit;
...
function to_BCD(N: natural; radix: integer := 10)
return BCD_number
is
variable digits: BCD_number (0 to 31); -- big enough for 2^31
variable more_digits: integer := N;
begin
assert (radix >= 2) and (radix <= 16)
report "Bad radix, must be between 2 and 16"
severity ERROR;
for i in digits'range loop
digits(i) := std_logic_vector(
to_unsigned(more_digits rem radix, BCD_digit'length) );
more_digits := more_digits / radix;
if (more_digits = 0) then
return digits(0 to i);
end if;
end loop;
report "Overflow in to_BCD" severity ERROR;
return (0 => "----");
end function to_BCD;

enjoy :)
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 
Thank you. It seems that is what I was looking for. :)

It is an integer signal that holds the "i" of a counter. The counter
goes from 0 to 2046. I needed each digit separately to display it on
the LCD screen that has the Spartan-3A board.


--m m _s
-----------------
On Jul 13, 8:07 am, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:
On Sat, 12 Jul 2008 21:31:06 -0700 (PDT), --mms <msmeer...@gmail.com
wrote:

So you're trying to convert decimal to BCD ("binary coded decimal").

There's a neat shift-and-subtract trick that has been mentioned here
many times before, and maps easily to hardware - google for
"binary to BCD fpga" should do it.

How is your integer represented?  Is it a binary number (an
integer signal or variable, or a std_logic_vector or numeric_std
signal or variable?

A non-synthesisable version might look like this - note it will do
hex, binary and octal conversion as well...

  subtype BCD_digit is std_logic_vector(3 downto 0);
  type BCD_number is array (natural range <>) of BCD_digit;
  ...
  function to_BCD(N: natural; radix: integer := 10)
    return BCD_number
  is
    variable digits: BCD_number (0 to 31); -- big enough for 2^31
    variable more_digits: integer := N;
  begin
    assert (radix >= 2) and (radix <= 16)
      report "Bad radix, must be between 2 and 16"
      severity ERROR;
    for i in digits'range loop
      digits(i) := std_logic_vector(
             to_unsigned(more_digits rem radix, BCD_digit'length) );
      more_digits := more_digits / radix;
      if (more_digits = 0) then
        return digits(0 to i);
      end if;
    end loop;
    report "Overflow in to_BCD" severity ERROR;
    return (0 => "----");
  end function to_BCD;

enjoy :)
--
Jonathan Bromley, Consultant
 
On Jul 13, 5:41 pm, m__ m___s <msmeer...@gmail.com> wrote:
Thank you. It seems that is what I was looking for. :)

It is an integer signal that holds the "i" of a counter. The counter
goes from 0 to 2046. I needed each digit separately to display it on
the LCD screen that has the Spartan-3A board.
There is more than one way to skin a cat. If you are incrementing the
counter, then you can just form a BCD counter and increment that.
That is likely going to use fewer resources than the conversion
code.

Rick
 
On Jul 13, 12:31 pm, --mms <msmeer...@gmail.com> wrote:
Hi:

I would like to know if there is any function/method, in VHDL, to
extract the digits of an integer.

What I wish to do is take, for example, an integer like 2043, and
extract first the number '3', then '4', '0', and '2'.

Each digit will then be converted to a std_logic_vector(3 downto 0).

Thank you!,
m m  s
you can have 4 counters for each integer.
each can advanced 1 when the before one reached 10.
 
http://www.mikrocontroller.net/attachment/19220/D3.1_Binary2BCD.ppt.pdf


On Jul 12, 11:31 pm, --mms <msmeer...@gmail.com> wrote:
(...)
 
--mms wrote:

I would like to know if there is any function/method, in VHDL, to
extract the digits of an integer.

What I wish to do is take, for example, an integer like 2043, and
extract first the number '3', then '4', '0', and '2'.

Each digit will then be converted to a std_logic_vector(3 downto 0).

http://en.wikipedia.org/wiki/Binary-coded_decimal
 

Welcome to EDABoard.com

Sponsor

Back
Top