convert a String to stdt_logic_vector

Discussion in 'VHDL' started by Klaus Sandner, Aug 10, 2007.

  1. I want to send a textstring to a display.To do this I have to transform
    each charakters of the string to 8 bit values.

    here my Vhdl solution

    constant LCD_display_len:positive:=16;
    subtype LCD_display_string is string(1 to LCD_display_len);
    subtype LCD_display_bit is std_logic_vector(1 to LCD_display_len*8);


    process (clk)
    variable data_string :LCD_display_string := "test string for ";
    variable data_bit :LCD_display_bit;

    now I have problems to write the right instruction

    data_bit <= conv ????????????????????????????

    has somebody a solution?


    klaus


    end process;
     
    Klaus Sandner, Aug 10, 2007
    #1
    1. Advertising

  2. Klaus Sandner

    Tricky Guest

    Re: convert a String to stdt_logic_vector

    On Aug 10, 4:38 pm, Klaus Sandner <> wrote:
    > I want to send a textstring to a display.To do this I have to transform
    > each charakters of the string to 8 bit values.
    >
    > here my Vhdl solution
    >
    > constant LCD_display_len:positive:=16;
    > subtype LCD_display_string is string(1 to LCD_display_len);
    > subtype LCD_display_bit is std_logic_vector(1 to LCD_display_len*8);
    >
    > process (clk)
    > variable data_string :LCD_display_string := "test string for ";
    > variable data_bit :LCD_display_bit;
    >
    > now I have problems to write the right instruction
    >
    > data_bit <= conv ????????????????????????????
    >
    > has somebody a solution?
    >
    > klaus
    >
    > end process;


    you need a function that goes something like this.

    --converts a std_logic_vector into a string.
    function slv_to_string(slv : std_logic_vector) return string is
    variable retString : string(1 to slv'length);
    begin
    for i in 0 to slv'length-1 loop
    if slv(i) = '1' then

    --ensure we dont write the string back to front!
    if slv'ASCENDING then
    retString(i+1) := '1'; --always +1 as the string range is 1 to
    slv'length
    else
    retString(retString'high-i) := '1';
    end if;
    else
    if slv'ASCENDING then
    retString(i+1) := '0'; --always +1 as the string range is 1 to
    slv'length
    else
    retString(retString'high-i) := '0';
    end if;
    end if;
    end loop;

    return retString;
    end function slv_to_string;
     
    Tricky, Aug 10, 2007
    #2
    1. Advertising

  3. Klaus Sandner

    Tricky Guest

    Re: convert a String to stdt_logic_vector

    Whoops I did that the wrong way round.

    The IEEE.std_logic_textio package has the ability to read/write
    std_logic_vectors from/to strings. These are procedures though. If you
    need a function:

    function string_to_slv(s : string) return std_logic_vector is
    variable ret_slv : std_logic_vector(s'length-1 downto 0);
    begin
    for i in 1 to s'length loop
    if s(i) = '0' then
    ret_slv(i-1) := '0';
    elsif s(i) = '1' then
    ret_slv(i-1) := '1';
    else
    --catch bad characters
    ret_slv(i-1) := 'X';
    end if;

    return ret_slv;
    end loop;
    end function;
     
    Tricky, Aug 10, 2007
    #3
  4. Klaus Sandner

    Tricky Guest

    Re: convert a String to stdt_logic_vector

    OMG. Im having a bad day:

    try 0 to s'length-1 as the ret_slv demensions, otherwise your bus
    will be returned back to front (and in your case not work at all)
     
    Tricky, Aug 10, 2007
    #4
  5. Re: convert a String to stdt_logic_vector

    i think your proposal is not right for my problem

    if s(i) = '0' then
    > ret_slv(i-1) := '0';
    > elsif s(i) = '1' then
    > ret_slv(i-1) := '1';



    I don't understand the conversion s(i) ='0'
    s(i) ='1'
    s is a string and the position if i is an aski byte








    Tricky schrieb:
    > Whoops I did that the wrong way round.
    >
    > The IEEE.std_logic_textio package has the ability to read/write
    > std_logic_vectors from/to strings. These are procedures though. If you
    > need a function:
    >
    > function string_to_slv(s : string) return std_logic_vector is
    > variable ret_slv : std_logic_vector(s'length-1 downto 0);
    > begin
    > for i in 1 to s'length loop
    > if s(i) = '0' then
    > ret_slv(i-1) := '0';
    > elsif s(i) = '1' then
    > ret_slv(i-1) := '1';
    > else
    > --catch bad characters
    > ret_slv(i-1) := 'X';
    > end if;
    >
    > return ret_slv;
    > end loop;
    > end function;
    >
    >
    >
     
    Klaus Sandner, Aug 10, 2007
    #5
  6. Re: convert a String to stdt_logic_vector

    i think your proposal is not right for my problem

    if s(i) = '0' then
    > ret_slv(i-1) := '0';
    > elsif s(i) = '1' then
    > ret_slv(i-1) := '1';



    I don't understand the conversion s(i) ='0'
    s(i) ='1'
    s is a string and the position if i is an aski byte








    Tricky schrieb:
    > Whoops I did that the wrong way round.
    >
    > The IEEE.std_logic_textio package has the ability to read/write
    > std_logic_vectors from/to strings. These are procedures though. If you
    > need a function:
    >
    > function string_to_slv(s : string) return std_logic_vector is
    > variable ret_slv : std_logic_vector(s'length-1 downto 0);
    > begin
    > for i in 1 to s'length loop
    > if s(i) = '0' then
    > ret_slv(i-1) := '0';
    > elsif s(i) = '1' then
    > ret_slv(i-1) := '1';
    > else
    > --catch bad characters
    > ret_slv(i-1) := 'X';
    > end if;
    >
    > return ret_slv;
    > end loop;
    > end function;
    >
    >
    >
     
    Klaus Sandner, Aug 10, 2007
    #6
  7. String to std_logic proposal may be wrong

    i think your proposal is not right for my problem

    if s(i) = '0' then
    > ret_slv(i-1) := '0';
    > elsif s(i) = '1' then
    > ret_slv(i-1) := '1';



    I don't understand the conversion s(i) ='0'
    s(i) ='1'
    s is a string and the position if i is an aski byte








    Tricky schrieb:
    > Whoops I did that the wrong way round.
    >
    > The IEEE.std_logic_textio package has the ability to read/write
    > std_logic_vectors from/to strings. These are procedures though. If you
    > need a function:
    >
    > function string_to_slv(s : string) return std_logic_vector is
    > variable ret_slv : std_logic_vector(s'length-1 downto 0);
    > begin
    > for i in 1 to s'length loop
    > if s(i) = '0' then
    > ret_slv(i-1) := '0';
    > elsif s(i) = '1' then
    > ret_slv(i-1) := '1';
    > else
    > --catch bad characters
    > ret_slv(i-1) := 'X';
    > end if;
    >
    > return ret_slv;
    > end loop;
    > end function;
    >
    >
    >
     
    Klaus Sandner, Aug 10, 2007
    #7
  8. Klaus Sandner

    Tricky Guest

    Re: String to std_logic proposal may be wrong

    Ahh, Now I think I get the problem.

    You cannot synthesize type STRING. You will have to make it a constant
    or generic, and then have a function something like this to generate
    the SLV you want to use:

    function string_to_slvString(s : string) : return std_logic_vector
    variable ret_slv : std_logic_vector(1 to s'length*8);
    begin

    for i in 0 to s'length-1 loop
    --unfortunatly you will have to do every single character conversion
    manually:
    case s(i) is
    when 'a' => ret_slv( (i*8) + 1 to (i+1)*8 ) <= x"00";
    when 'b' => ret_slv( (i*8) + 1 to (i+1)*8 ) <= x"01";
    --
    --
    --
    --etc
    end case
    end loop;

    return ret_slv;

    end function;


    and then call it like this:

    constant data_string :LCD_display_string := "test string for ";
    constant LCD_display_bit is std_logic_vector(1 to
    LCD_display_len*8) := string_to_slvString(data_string);
     
    Tricky, Aug 13, 2007
    #8
  9. Klaus Sandner

    Russell

    Joined:
    Aug 17, 2007
    Messages:
    6
    the following will convert a character to an 8 bit ascii vector:

    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;
    the 'pos attribute will give the 'position' of a character in the ascii table
    example usage:
    data <= ch2vec(char); where char is say, a signal of type character eg. "A"
     
    Russell, Aug 18, 2007
    #9
    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. Andrew Banks

    Convert string to a string array

    Andrew Banks, Apr 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    58,108
    Andrew Banks
    Apr 19, 2004
  2. =?Utf-8?B?TWlrZSBNb29yZQ==?=

    Connection String object Convert to String Variable Type

    =?Utf-8?B?TWlrZSBNb29yZQ==?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,089
    Norman Yuan
    Oct 26, 2004
  3. rockdale

    Convert formatted string to string?

    rockdale, Apr 11, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    433
    rockdale
    Apr 12, 2006
  4. Phd
    Replies:
    3
    Views:
    1,157
    Steven Bethard
    Dec 6, 2004
  5. Replies:
    2
    Views:
    195
    John Bokma
    Aug 30, 2013
Loading...

Share This Page