Java examples for java.lang:byte Array to int
Custom algorithm used to generate an int from a series of bytes.
/*/*from ww w . j av a 2 s .com*/ * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. */ //package com.java2s; public class Main { public static void main(String[] argv) throws Exception { byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out.println(toInt(bytes)); } /** * Custom algorithm used to generate an int from a series of bytes. * <p/> * NOTE : this is different than interpreting the incoming bytes as an int value! * * @param bytes The bytes to use in generating the int. * * @return The generated int. */ public static int toInt(byte[] bytes) { int result = 0; for (int i = 0; i < 4; i++) { result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i]; } return result; } }