title: How to convert a binary value to ASCII in JAVA?
body: <p>This is a homework assignment that I can't wrap my head around.  I have to do it manually, so I can't use "getBytes()."  Also, I have to convert to decimal format first, and then convert the decimal to ASCII (e.g. {0,1,1,0,0,0,1,0} = 98 in decimal format, which is a 'b').  I have arranged the binary code into an array, and want to use a for loop to traverse the array position by position.  However, I'm not sure I'm using the correct parameters for the for loop, and am not sure how to divide the code into bits of "8."  Another thing, how to I convert the decimal value to ASCII?  Should I just list out all the possible letters I know I will get, and then refer to them using an if-else loop?  Or could I just convert the decimal to a char?  Here is my code so far... (It's a bit messy, sorry)</p>
  
  <pre><code>class Decoder
  {
      public void findCode(Picture stegoObj)
      {
      Pixel pixTar = new Pixel(stegoObj,0,0);
      Pixel [] pixelArray = stegoObj.getPixels();
      int blueVal = 0;
  
      for(int length = 0; length &lt; pixelArray.length; length++)
      {
          blueVal = pixTar.getBlue();                
      }
      System.out.println(blueVal);
      stegoObj.explore();
      }
  
      public void decode(int [] binary)
      {
          int binaryLen = binary.length;
          int totVal = 0;
          int newVal = 0;
          int bitVal = 0;
  
          for(int x = binaryLen - 1; x &gt;= 0; x--)
          {
              bitVal = binary[x];
              int exp = x - (binaryLen - 1);
              totVal += (pow(bitVal, exp));
          }
  
          System.out.println(totVal);
       }
  }
  public class DecodeImage
  {
      public static void main(String[] args)
      {
          Picture stegoObj = new Picture("SecretMessage.bmp");
          Decoder deco = new Decoder();
          int[] binArray =     {0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,1,0,0,1,0,0,1,1,0,1,1,0,0,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,0,1,1,0,0,1,0,1,0,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0};
          //int[] binArray = {0,1,1,0,0,0,1,0};
          //deco.findCode(stegoObj);
          deco.decode(binArray);        
      }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Okay, so I figured out this much, under the class Decoder, in the decode block, in the for loop:                                       </p>
  
  <pre><code>for(int x = binaryLen - 1; x &gt;= 0; x--)
  {
      bitVal = binary[x];
      preVal = bitVal * base;
      totVal += preVal;
      base = base * 2;
  }
  </code></pre>