Reads a Big Endian DWORD value from a byte array. - Java Internationalization

Java examples for Internationalization:Big Endian Little Endian

Description

Reads a Big Endian DWORD value from a byte array.

Demo Code


//package com.java2s;

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

    /**
     * Reads a Big Endian DWORD value from a byte array.
     *
     * @param data The byte array from which the DWORD value is read.
     * @param offset The index of the array element where DWORD reading begins.
     *
     * @return The DWORD value read from the array.
     */
    public static long readDwordBigEndian(final byte[] data,
            final int offset) {
        return (data[offset + 0] & 0xFFL) * 0x100 * 0x100 * 0x100
                + (data[offset + 1] & 0xFFL) * 0x100 * 0x100
                + (data[offset + 2] & 0xFFL) * 0x100
                + (data[offset + 3] & 0xFFL);
    }
}

Related Tutorials