All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.util.BinaryUtil Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense 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.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense 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 AgroSense.  If not, see .
 */
package nl.cloudfarming.client.util;

/**
 *
 * @author Gerben Feenstra
 */
public class BinaryUtil {
    
    /**
     * Converts a int to a byte array containing 4 bytes (representing a 32bit integer)
     * @param value the int to convert
     * @return the converted value
     */
    public static byte[] intToByteArray(int value) {
        return new byte[]{
                    (byte) (value >>> 24),
                    (byte) (value >>> 16),
                    (byte) (value >>> 8),
                    (byte) value};
    }

    /**
     * Converts a byte array containing 4 bytes to a 32bit integer
     * @param b the byte array to convert
     * @return the converted byte array
     */
    public static int byteArrayToInt(byte[] b) {
        return (b[0] << 24)
                + ((b[1] & 0xFF) << 16)
                + ((b[2] & 0xFF) << 8)
                + (b[3] & 0xFF);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy