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

Java examples for Internationalization:Big Endian Little Endian

Description

Reads a Little 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(readDwordLittleEndian(data, offset));
    }/*from  w ww .j  a  v a  2  s. co  m*/

    /**
     * Reads a Little 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 readDwordLittleEndian(final byte[] data,
            final int offset) {
        return (data[offset + 3] & 0xFFL) * 0x100 * 0x100 * 0x100
                + (data[offset + 2] & 0xFFL) * 0x100 * 0x100
                + (data[offset + 1] & 0xFFL) * 0x100
                + (data[offset + 0] & 0xFFL);
    }
}

Related Tutorials