get Long Value From Byte Array Big Endian - Java Internationalization

Java examples for Internationalization:Charset

Description

get Long 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(getLongValueFromByteArrayBigEndian(byteArray,
                offset));/*w  ww.  j  ava  2s  . c  o  m*/
    }

    public static long getLongValueFromByteArrayBigEndian(byte[] byteArray,
            int offset) {
        long val = 0;
        val = ((((long) byteArray[offset + 7]) << 8 >> 8) & 0xFFL)
                + ((((long) byteArray[offset + 6]) << 8) & 0xFF00L)
                + ((((long) byteArray[offset + 5]) << 16) & 0xFF0000L)
                + ((((long) byteArray[offset + 4]) << 24) & 0xFF000000L)
                + ((((long) byteArray[offset + 3]) << 32) & 0xFF00000000L)
                + ((((long) byteArray[offset + 2]) << 40) & 0xFF0000000000L)
                + ((((long) byteArray[offset + 1]) << 48) & 0xFF000000000000L)
                + ((((long) byteArray[offset]) << 56) & 0xFF00000000000000L);
        return val;
    }
}

Related Tutorials