title: Convert binary ( integer and fraction) from VHDL to decimal, negative value in C code
body: <p>I have a 14-bit data that is fed from FPGA in vhdl, The NIos II processor reads the 14-bit data from FPGA and do some processing tasks, where Nios II system is programmed in C code</p>
  
  <p>The 14-bit data can be positive, zero or negative. In Altera compiler, I can only define the data to be 8,16 or 32. So I define this to be 16 bit data.</p>
  
  <p>First, I need to check if the data is negative, if it is negative, I need to pad the first two MSB to be bit '1' so the system detects it as negative value instead of positive value. </p>
  
  <p>Second, I need to compute the real value of this binary representation into a decimal value of BOTH integer and fraction.</p>
  
  <p>I learned from this link (<a href="https://stackoverflow.com/questions/15767758/correct-algorithm-to-convert-binary-floating-point-1101-11-into-decimal-13-75">Correct algorithm to convert binary floating point &quot;1101.11&quot; into decimal (13.75)?</a>) that I could convert a binary (consists of both integer and fraction) to decimal values.</p>
  
  <p>To be specified, I am able to use this code quoted from this link (<a href="https://stackoverflow.com/questions/15767758/correct-algorithm-to-convert-binary-floating-point-1101-11-into-decimal-13-75">Correct algorithm to convert binary floating point &quot;1101.11&quot; into decimal (13.75)?</a>) , reproduced as below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  double convert(const char binary[]){
    int bi,i;
    int len = 0;
    int dot = -1;
    double result = 0;
  
    for(bi = 0; binary[bi] != '\0'; bi++){
      if(binary[bi] == '.'){
        dot = bi;
      }
      len++;
    }
    if(dot == -1)
      dot=len;
  
    for(i = dot; i &gt;= 0 ; i--){
      if (binary[i] == '1'){
        result += (double) pow(2,(dot-i-1));
      }
    }
    for(i=dot; binary[i] != '\0'; i++){
      if (binary[i] == '1'){
        result += 1.0/(double) pow(2.0,(double)(i-dot));
      }
    }
    return result;
  }
  
  int main()
  {
     char  bin[] = "1101.11";
     char  bin1[] = "1101";
     char  bin2[] = "1101.";
     char  bin3[] = ".11";
  
     printf("%s -&gt; %f\n",bin, convert(bin)); 
     printf("%s -&gt; %f\n",bin1, convert(bin1)); 
     printf("%s -&gt; %f\n",bin2, convert(bin2)); 
     printf("%s -&gt; %f\n",bin3, convert(bin3)); 
  
     return 0;
  }
  </code></pre>
  
  <p>I am wondering if this code can be used to check for negative value? I did try with a binary string of 11111101.11 and it gives the output of 253.75... </p>
  
  <p>I have two questions:</p>
  
  <ol>
  <li>What are the modifications I need to do in order to read a negative value? </li>
  </ol>
  
  <p>I know that I can do the bit shift (as below) to check if the msb is 1, if it is 1, I know it is negative value... </p>
  
  <pre><code>if (14bit_data &amp; 0x2000) //if true, it is negative value
  </code></pre>
  
  <p>The issue is, since it involves fraction part (but not only integer), it confused me a bit if the method still works...</p>
  
  <ol start="2">
  <li>If the binary number is originally not in string format, is there any way I could convert it to string? The binary number is originally fed from a fpga block written in VHDL say, 14 bits, with msb as the sign bit, the following 6 bits are the magnitude for integer and the last 6 bits are the magnitude for fractional part. I need the decimal value in C code for Altera Nios II processor.</li>
  </ol>