Velocity Reviews > VHDL > Extracting digits [0-9] from an number/integer

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

--mms
Guest
Posts: n/a

 07-13-2008
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

jeppe
Senior Member
Join Date: Mar 2008
Location: Denmark
Posts: 348

 07-13-2008
You need to concider a binary to BCD conversion.
The best way to perform this will an old trick from the 60ies I believe involving a slithlig modified shiftregister.

The method can be found here:
http://www.jjmk.dk/MMMI/Lessons/06_A...sion/Index.htm
Jeppe

m__ m___s
Guest
Posts: n/a

 07-13-2008
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 <(E-Mail Removed)>
wrote:
> On Sat, 12 Jul 2008 21:31:06 -0700 (PDT), --mms <(E-Mail Removed)>
> 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
> * * * 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

rickman
Guest
Posts: n/a

 07-13-2008
On Jul 13, 5:41 pm, m__ m___s <(E-Mail Removed)> 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

jeppe
Senior Member
Join Date: Mar 2008
Location: Denmark
Posts: 348

 07-14-2008
More then one way to skin a cat ....

I agree - it will properly cost you less logic if you implement a binary counter and a BCD counter as well

Jeppe

Victor
Guest
Posts: n/a

 07-14-2008
On Jul 13, 12:31*pm, --mms <(E-Mail Removed)> 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.

m m
Guest
Posts: n/a

 07-23-2008

http://www.mikrocontroller.net/attac...ry2BCD.ppt.pdf

On Jul 12, 11:31*pm, --mms <(E-Mail Removed)> wrote:
(...)

Mike Treseler
Guest
Posts: n/a

 07-23-2009
--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