+ Post New Thread
Results 1 to 7 of 7
  1. #1
    Newbie level 3
    Points: 25, Level: 1

    Join Date
    Apr 2015
    Posts
    3
    Helped
    0 / 0
    Points
    25
    Level
    1

    If I have 12 bits each 4 represent a number in binary, how do i convert it to binary?

    Hi,

    How can I convert a 12 bit binary number where each 4 bits represent a character into an 8 bit binary number?

    Example: 12 bit number : "001001000011" ==> 2,4,3 and I want to convert it to 243 ==> "11110011"

    any help please?

    What I did but still haven't reached a right answer:
    Code VHDL - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    
    signal output1, output2, output3 : integer range 0 to 9;
    signal output11, output22 : integer range 0 to 255;
     
    begin
     
    process(clk)
    begin
    if(clk'event and clk='1') then  
        case input(11 downto 8) is
     
            when  "0000" => output1 <= 0; 
            when  "0001" => output1 <= 1;
            when  "0010" => output1 <= 2;
            when  "0011" => output1 <= 3;
            when  "0100" => output1 <= 4;
            when  "0101" => output1 <= 5;
            when  "0110" => output1 <= 6;
            when  "0111" => output1 <= 7;
            when  "1000" => output1 <= 8;
            when  "1001" => output1 <= 9;
            when  others => output1 <= 0;
        end case;
        
        case input(7 downto 4) is
     
            when  "0000" => output2 <= 0; 
            when  "0001" => output2 <= 1;
            when  "0010" => output2 <= 2;
            when  "0011" => output2 <= 3;
            when  "0100" => output2 <= 4;
            when  "0101" => output2 <= 5;
            when  "0110" => output2 <= 6;
            when  "0111" => output2 <= 7;
            when  "1000" => output2 <= 8;
            when  "1001" => output2 <= 9;
            when  others => output2 <= 0;
        end case;
        
        case input(3 downto 0) is
     
            when  "0000" => output3 <= 0; 
            when  "0001" => output3 <= 1;
            when  "0010" => output3 <= 2;
            when  "0011" => output3 <= 3;
            when  "0100" => output3 <= 4;
            when  "0101" => output3 <= 5;
            when  "0110" => output3 <= 6;
            when  "0111" => output3 <= 7;
            when  "1000" => output3 <= 8;
            when  "1001" => output3 <= 9;
            when  others => output3 <= 0;
        end case;
    end if;
    end process;
     
    output11 <= output1*100;
    output22 <= output2*10;
    finaloutput <= output1+output2+output3;
    output<= std_logic_vector(to_unsigned(finaloutput,8));
    Last edited by FvM; 4th May 2015 at 11:13. Reason: Added syntax tags

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 260,203, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,441
    Helped
    13825 / 13825
    Points
    260,203
    Level
    100

    Re: If I have 12 bits each 4 represent a number in binary, how do i convert it to bin

    What do you mean by "still haven't reached a right answer"? What are your requirements for a "right answer"?

    Altough the BCD to binary operation can be described in less lines using ieee.numeric_std conversion functions, the shown code should basically work.



  3. #3
    Newbie level 3
    Points: 25, Level: 1

    Join Date
    Apr 2015
    Posts
    3
    Helped
    0 / 0
    Points
    25
    Level
    1

    Re: If I have 12 bits each 4 represent a number in binary, how do i convert it to bin

    Yes I know, I tried using the conversion function but it didn't work so I used the long conversion.

    I don't have a specific criteria, It's just that after I use a test bench I don't get the correct answer.



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 260,203, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,441
    Helped
    13825 / 13825
    Points
    260,203
    Level
    100

    Re: If I have 12 bits each 4 represent a number in binary, how do i convert it to bin

    It's just that after I use a test bench I don't get the correct answer.
    But why? We need to see the complete entity and testbench to understand the reason.



  5. #5
    Full Member level 6
    Points: 3,477, Level: 13
    Achievements:
    7 years registered
    vipinlal's Avatar
    Join Date
    Mar 2010
    Location
    India
    Posts
    354
    Helped
    76 / 76
    Points
    3,477
    Level
    13

    Re: If I have 12 bits each 4 represent a number in binary, how do i convert it to bin

    you are looking for a BCD to binary converter. Try this code:

    http://vhdlguru.blogspot.in/2015/04/...onversion.html
    If we admit that human life can be ruled by reason, then all possibility of life is destroyed.



  6. #6
    Super Moderator
    Points: 31,597, Level: 43
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    7,307
    Helped
    1719 / 1719
    Points
    31,597
    Level
    43

    Re: If I have 12 bits each 4 represent a number in binary, how do i convert it to bin

    The only issue I see with the OP's code is the range of output1 should be restricted to 0,1, or 2 as the range of the result for output11 can not be outside the range of 0 to 255. Perhaps the range restrictions are the root of the problem.



    •   AltAdvertisement

        
       

  7. #7
    Super Moderator
    Points: 260,203, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,441
    Helped
    13825 / 13825
    Points
    260,203
    Level
    100

    Re: If I have 12 bits each 4 represent a number in binary, how do i convert it to bin

    Perhaps the range restrictions are the root of the problem.
    I don't think so. In synthesis, casting an integer to unsigned with insufficient length would just cause a wrap around respectively discarding of the upper two bits, e.g. 256 is output as zero.

    A simulator stops with an error when the overflow occurs.



--[[ ]]--