Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

type conversion in VHDL. integer -> string/character

2,368 views
Skip to first unread message

Janaka Withana

unread,
Jul 16, 1993, 6:09:23 PM7/16/93
to
is there a simple way of coverting between not-very-similar data
types? specifically, i want to convert from an integer to a string
(or character). i need to do this because the integer is passed in as
a generic and i want to open a file that has the integer value in the
filename. the following (i.e., string(num)) will not work:

------------------------------------------------------------------------------

entity ent is

generic(
num: integer range 0 to 3 := 0
);

port(
clk: in mvl5w;
data: in mvl5w_vector(15 downto 0);
valid: in mvl5w;
kill: in mvl5w
);

end ent;

Architecture behavior of ent is

file outfile: text is out "hello" & string(num); -- doesn't work

begin

...

end behavior;

------------------------------------------------------------------------------

sorry if this is FAQ, but i couldn't find anything in the LRM or other
VHDL texts. of course i could pass in the number as a character or
string, but i'd rather not as it gets used in integer operations
later. i guess i could also write my own function...

advTHANKSance

--janaka
--
-----------------------------------------------------------------------------
| jwit...@phoenix.princeton.edu jan...@lucille.princeton.edu |
| jan...@ee.princeton.edu JWIT...@PUCC.bitnet m...@clarity.princeton.edu |
-----------------------------------------------------------------------------

Paul J Menchini -- Personal Account

unread,
Jul 17, 1993, 4:32:14 PM7/17/93
to
In article <1993Jul16.2...@Princeton.EDU> jan...@olympus.Princeton.EDU (Janaka Withana) writes:
>
>is there a simple way of coverting between not-very-similar data
>types? specifically, i want to convert from an integer to a string
>(or character).

These are not closely related types (see recent posting by me), so you can't
use a type conversion, as you tried.

You can write your own function, or use TextIO.

Here's a function for Naturals:

function cvString (N, L: Natural) return String
-- Accepts a number N to convert into a string of length L. If the resulting
-- string is too small, you get the least-significant digits of N in the
-- returned string.
is
variable Temp: Natural := N;
variable Result: String(1 to L) := (others => ' ');
begin
for i in Result'reverse_range loop
Result(i) := Character'Val((Temp mod 10) + Character'Pos('0'));
Temp := Temp/10;
exit when Temp = 0;
end loop;
return Result;
end cvString;

Extend to integers if you're going negative.

Here's a function to tell you what L should be for a given N:

function Log10 (N: Natural) return Natural is
variable Temp: Natural := N;
variable Result: Natural := 1;
begin
loop
Temp := Temp/10;
exit when Temp = 0;
Result := Result + 1;
end loop;
return Result;
end Log10;

--Paul
--
Paul Menchini email: me...@rock.concert.net "Pay no attention to
2 Davis Drive voice: 919-990-9506 that man behind the
P.O. Box 13036 fax: 919-990-8561 curtain!"
RTP, NC 27709-3036 -- The Wizard of Oz

0 new messages