Java Array Normalize normalize(final byte[] input, final int bit)

Here you can find the source of normalize(final byte[] input, final int bit)

Description

Normalizes a ByteArray and fits it in the given range.

License

Open Source License

Parameter

Parameter Description
input The ByteArray.
bit The range is 0 to Math.pow(2, bit).

Return

The normalized value of the ByteArray.

Declaration

public static final byte[] normalize(final byte[] input, final int bit) 

Method Source Code

//package com.java2s;
/**//ww w  .j a  va  2  s.  c om
 *  Copyright (C) 2012 Ulrich Viefhaus
 *    This file (ByteUtils.java) is from the package utils which is part of Hashchecker.
 *    Hashchecker is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Hashmaker and Hashviewer is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Hashmaker and Hashviewer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

public class Main {
    /**
     * Normalizes a ByteArray and fits it in the given range.
     * 
     * @param input
     *            The ByteArray.
     * @param bit
     *            The range is 0 to Math.pow(2, bit).
     * @return The normalized value of the ByteArray.
     */
    public static final byte[] normalize(final byte[] input, final int bit) {

        byte[] tmp = input;
        final double max = Math.pow(2, bit);
        int out = ((int) (byteArrayToInt(input) % max));
        if (out < 0) {
            out += max;
        }
        tmp = intToByteArray((out));

        return tmp;
    }

    /**
     * Converts a ByteArray to an Integer.
     * 
     * @param input
     *            The ByteArray
     * @return The corresponding Integer.
     */
    public static final Integer byteArrayToInt(final byte[] input) {

        return (input[0] << 24) + ((input[1] & 0xFF) << 16)
                + ((input[2] & 0xFF) << 8) + (input[3] & 0xFF);
    }

    /**
     * @param value
     *            The Integer you want to convert.
     * @return The Byte Array representation of the given Integer.
     */
    public static final byte[] intToByteArray(final int value) {

        return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16),
                (byte) (value >>> 8), (byte) value };
    }
}

Related

  1. normalize(double[] x)
  2. normalize(double[] xs)
  3. normalize(double[][] matrix, double lower, double upper)
  4. normalize(double[][] result)
  5. normalize(double[][] X)
  6. normalize(final double[] a)
  7. normalize(final double[] doubles)
  8. normalize(final double[] fir)
  9. normalize(final double[] tab)