get Int Value From Byte Array Big Endian - Java Internationalization

Java examples for Internationalization:Charset

Description

get Int Value From Byte Array Big Endian

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] byteArray = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int offset = 2;
        System.out.println(getIntValueFromByteArrayBigEndian(byteArray,
                offset));/*from w w  w. j  a  v  a2s  . c  o  m*/
    }

    public static int getIntValueFromByteArrayBigEndian(byte[] byteArray,
            int offset) {
        int val = 0;
        val = ((byteArray[offset + 3] << 8 >> 8) & 0xFF)
                + ((byteArray[offset + 2] << 8) & 0xFF00)
                + ((byteArray[offset + 1] << 16) & 0xFF0000)
                + ((byteArray[offset] << 24) & 0xFF000000);
        return val;
    }
}

Related Tutorials