title: How to unpack COMP-3 digits using Java?
body: <p>I have huge mainframe file and there are some packed digits in that file. I would like to know how to unpack following digit using java?</p>
  
  <p>packed digit : <code>?</code></p>
  
  <p>I read tutorials for unpacking digits and found the following rule to count the number of bytes required to unpack digits :</p>
  
  <p><code>total_number_of_bytes = (no. of digits + 1) / 2</code></p>
  
  <p>I wrote the following code to unpack digits :</p>
  
  <pre><code>public String unpackData(String packedData, int decimalPointLocation) {
          String unpackedData = "";
          char[] characters = packedData.toCharArray();
          final int impliedPositive = 15;
          final int positiveNumber = 12;
          final int negativeNumber = 13;
          for (int currentCharIndex = 0; currentCharIndex &lt; characters.length; currentCharIndex++) {
              byte[] unpackedDigits = unpackByte((byte) characters[currentCharIndex]);
              if(currentCharIndex == (characters.length - 1)) {
                  if(unpackedDigits[1] == impliedPositive || unpackedDigits[1] == positiveNumber) {
                      unpackedData += String.valueOf(unpackedDigits[0]);
                  } else if(unpackedDigits[1] == negativeNumber) {
                      unpackedData = "-" + unpackedData;
                  }
              } else {
                  unpackedData += String.valueOf(unpackedDigits[0]) + String.valueOf(unpackedDigits[1]);
              }
          }
          if(decimalPointLocation &gt; 0) {
              unpackedData = unpackedData.substring(0, (decimalPointLocation - 1)) + 
                              "." + 
                              unpackedData.substring(decimalPointLocation);
          }
          return unpackedData;
      }
  
      private byte[] unpackByte(byte packedData) {
          byte firstDigit = (byte) (packedData &gt;&gt;&gt; 4);
          firstDigit = setBitsToZero(firstDigit, 4, 8);
  
          //System.out.println(" firstDigit = "+ firstDigit + ", and its bit string after unpacking = " + getBitString(firstDigit, 7));
  
          byte secondDigit = setBitsToZero(packedData, 4, 8);
          //System.out.println("second digit = " + secondDigit + ", and its bit string of second digit after unpcking = " + getBitString(secondDigit, 7));
  
          byte[] unpackedData = new byte[2];
          unpackedData[0] = firstDigit;
          unpackedData[1] = secondDigit;
          return unpackedData;
      }
  
      private byte setBitsToZero(byte number, int startBitPosition, int endBitPosition) {
          for (int i = startBitPosition; i &lt; endBitPosition; i++) {
              number =  (byte) (number &amp; ~(1 &lt;&lt; i));
          }
          return number;
      }
  </code></pre>
  
  <p>This program works correctly for integer type values but it's not working for floating point type values.</p>
  
  <p>Can anyone please tell if my program is correct?</p>