Here you can find the source of normalize(final byte[] input, final int bit)
Parameter | Description |
---|---|
input | The ByteArray. |
bit | The range is 0 to Math.pow(2, bit). |
public static final byte[] normalize(final byte[] input, final int bit)
//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 }; } }