Java Integer Create toIntBigEndian(byte[] input)

Here you can find the source of toIntBigEndian(byte[] input)

Description

Convert a byte array of length 4 into an int number, using big-endian notation

License

Open Source License

Parameter

Parameter Description
input - the byte array

Return

the converted int or 0 if input.length != 4

Declaration

public static int toIntBigEndian(byte[] input) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 JCrypTool Team and Contributors
 * /*  w w w  .  ja v a  2s .c  om*/
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

public class Main {
    /**
     * Convert a byte array of length 4 into an int number, using big-endian notation
     * 
     * @param input - the byte array
     * @return the converted int or <tt>0</tt> if <tt>input.length != 4</tt>
     */
    public static int toIntBigEndian(byte[] input) {
        int result = 0;
        if (input.length != 4) {
            return 0;
        }
        result ^= (input[0] & 0xff) << 24;
        result ^= (input[1] & 0xff) << 16;
        result ^= (input[2] & 0xff) << 8;
        result ^= input[3] & 0xff;
        return result;
    }
}

Related

  1. toIntArray(String[] anArray)
  2. toIntArray(String[] array)
  3. toIntArray(String[] ss)
  4. toIntArrayUnshifted(byte... arguments)
  5. toIntBE(byte[] src, int offset)
  6. toIntDecoded(String s)
  7. toIntDefaultIfNull(Integer configured, int theDefault)
  8. toInteger(boolean b)
  9. toInteger(boolean bool)