convert Askistring to Hex

K

Klaus Sandner

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
 
Joined
Aug 17, 2007
Messages
6
Reaction score
0
function ch2vec (
Data : character)
return std_logic_vector is
variable ch : std_logic_vector(7 downto 0);
begin
ch := conv_std_logic_vector(character'pos(Data),8);
return ch;
end ch2vec;

usage:

constant intro_msg : string := "Hello there";

data <= ch2vec(intro_msg(char_number));
 
J

Jonathan Bromley

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

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
(e-mail address removed)
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top