convert Askistring to Hex

K

Klaus Sandner

Guest
I have to transform a string given in
Askiformat for example --- > " 3dies ist ein String"
in Hexformat result ----> X'2a30.......'
when in hexformat I have the possibility
to make an assignment of the form

-------------------------------------------------
result <= std_logic_vector(1 to stringlength*8);
--------------------------------------------------

for the tranformation I want use the library funktions.


Has somebody a solution with the library?


klaus
 
On Mon, 13 Aug 2007 09:23:23 +0200, Klaus Sandner
<klaus.sandner@t-online.de> wrote:

I have to transform a string given in
Askiformat for example --- > " 3dies ist ein String"
in Hexformat result ----> X'2a30.......'
when in hexformat I have the possibility
to make an assignment of the form

-------------------------------------------------
result <= std_logic_vector(1 to stringlength*8);
--------------------------------------------------
Since you didn't get any response, I'll try...

The guts of the problem is to extract the ASCII code
value for each character of the string. That's easily
done by using the 'POS attribute. Assembling the
std_logic_vector is then rather easy.

Finally, the truly brilliant thing about VHDL is the
way you can write the conversion in a completely
general way, so it will work on any string.

I've used a lot of intermediate variables to make the
code more obvious. You could easily get rid of them
by writing more complicated expressions.

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package string_utils is
function to_slv(s: string) return std_logic_vector;
end package;

package body string_utils is
function to_slv(s: string) return std_logic_vector is
--- Make sure the input string has sensible subscript range
constant ss: string(1 to s'length) := s;
--- Create a variable to hold the result.
variable answer: std_logic_vector(1 to 8*s'length);
--- Variable to simplify the horrible subscript calculations
variable p: integer;
--- Variable to hold each character's ASCII code
variable c: integer;
begin
for i in ss'range loop --- scan the characters
p := 8*i; ---index of right-most bit of 8-bit vector value
c := character'pos(ss(i)); --- get the character's ASCII code
answer(p-7 to p) := std_logic_vector(to_unsigned(c,8));
end loop;
return answer;
end;
end package body;


OK, so that only uses one library function: to_unsigned().
Dunno if that satisfies your (homework?) requirements.
--
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.
 

Welcome to EDABoard.com

Sponsor

Back
Top