Here you can find the source of toInt(final byte b)
Parameter | Description |
---|---|
b | the byte to convert |
public static int toInt(final byte b)
//package com.java2s; /*// www.j ava 2 s . c om (c) copyright This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class Main { /** * Constant to represent the big endian format (the format used internally * in Java). **/ public static boolean BIG_ENDIAN = true; /** * Converts an unsigned byte to an int, ie treating the bits in the byte * as the low 8 bits in the int (ie the eigth bit is not treated as a * sign bit). * * @param b the byte to convert * @return the resulting value as an int. **/ public static int toInt(final byte b) { return b & 0xff; } /** * Converts a byte[4] array to a integer by placing the bytes after * each other. The first byte in the array (byte[0]) becomes the most * significant byte in the resulting int ie big-endian * conversion. * * @param ba the array to be converted. * @return the resulting value as an int. **/ public static int toInt(final byte[] ba) { return toInt(ba, 0, BIG_ENDIAN); } /** * Converts a byte[offset+4] array to a integer by placing the bytes * after each other. The first byte in the array (byte[offset]) becomes the * most significant byte in the resulting int ie big-endian * conversion. * * @param ba the array to be converted. * @param offset starting offset in the array to be converted. * @return the resulting value as an int. **/ public static int toInt(final byte[] ba, final int offset) { return toInt(ba, offset, BIG_ENDIAN); } /** * Converts a byte[offset+4] array to a integer by placing the bytes * after each other. The first byte in the array (byte[offset]) becomes the * most significant byte in the resulting int ie big-endian * conversion. * * @param ba the array to be converted. * @param format the format of the byte array. * @return the resulting value as an int. **/ public static int toInt(final byte[] ba, final boolean format) { return toInt(ba, 0, format); } /** * Converts four bytes in a byte array to a integer by placing the * bytes after each other. I format is false the offset byte in the * array (byte[0]) becomes the most and byte offset+3 the least * significant byte in the resulting int, ie big-endian * conversion. if format is true the conversion is little-endian. * * Note: the BIG_ENDIAN and LITTLE_ENDIAN should be used in * the format parameter. * * @param ba the array to be converted. * @param offset starting offset in the array to be converted. * @param format the byte order of the byte array (see note above). * @return the resulting value as an int. **/ public static int toInt(final byte[] ba, final int offset, final boolean format) { if (format) { return (ba[0 + offset] & 0xff) | ((ba[1 + offset] & 0xff) << 8) | ((ba[2 + offset] & 0xff) << (8 * 2)) | ((ba[3 + offset] & 0xff) << (8 * 3)); } return ((ba[0 + offset] & 0xff) << (8 * 3)) | ((ba[1 + offset] & 0xff) << (8 * 2)) | ((ba[2 + offset] & 0xff) << 8) | (ba[3 + offset] & 0xff); } }