code: static public int bigDecimalToBytes(BigDecimal decimal, byte[] buffer, int index) {
          BigInteger intVal = decimal.unscaledValue();
          int length = 12 + (intVal.bitLength() + 8) / 8;
  
          int scale = decimal.scale();
          System.arraycopy(intToBytes(scale), 0, buffer, index, 4);  // copy in the scale
          index += 4;
  
          int precision = decimal.precision();
          System.arraycopy(intToBytes(precision), 0, buffer, index, 4);  // copy in the scale
          index += 4;
  
          System.arraycopy(bigIntegerToBytes(intVal), 0, buffer, index, length - 8);     // copy in the big integer
          return length;
      }
docs: This function converts a big decimal into its corresponding byte format and inserts
  it into the specified buffer at the specified index.
  
  @param decimal The big decimal to be converted.
  @param buffer The byte array.
  @param index The index in the array to begin inserting bytes.
  @return The number of bytes inserted.