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

Discussion in 'VHDL' started by --mms, Jul 13, 2008.

  1. --mms

    --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
    --mms, Jul 13, 2008
    #1
    1. Advertising

  2. --mms

    jeppe

    Joined:
    Mar 10, 2008
    Messages:
    348
    Location:
    Denmark
    jeppe, Jul 13, 2008
    #2
    1. Advertising

  3. --mms

    m__ m___s Guest

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

    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 <>
    wrote:
    > On Sat, 12 Jul 2008 21:31:06 -0700 (PDT), --mms <>
    > 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
    m__ m___s, Jul 13, 2008
    #3
  4. --mms

    rickman Guest

    On Jul 13, 5:41 pm, m__ m___s <> 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
    rickman, Jul 13, 2008
    #4
  5. --mms

    jeppe

    Joined:
    Mar 10, 2008
    Messages:
    348
    Location:
    Denmark
    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
    jeppe, Jul 14, 2008
    #5
  6. --mms

    Victor Guest

    On Jul 13, 12:31 pm, --mms <> 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.
    Victor, Jul 14, 2008
    #6
  7. --mms

    m m Guest

    m m, Jul 23, 2008
    #7
  8. --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
    Mike Treseler, Jul 23, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith Valentine
    Replies:
    5
    Views:
    18,463
    Keith Valentine
    Sep 20, 2004
  2. Kosio

    Extracting Digits from a Number

    Kosio, Sep 9, 2005, in forum: C Programming
    Replies:
    13
    Views:
    983
    Mike H
    Sep 12, 2005
  3. bejiz
    Replies:
    12
    Views:
    591
  4. Chris Rebert
    Replies:
    1
    Views:
    371
    Grant Edwards
    Nov 5, 2010
  5. Tim Chase
    Replies:
    2
    Views:
    533
    Tim Chase
    Nov 6, 2010
Loading...

Share This Page