Read 4 bytes in Little-endian byte order. - Java Internationalization

Java examples for Internationalization:Big Endian Little Endian

Description

Read 4 bytes in Little-endian byte order.

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 index = 2;
        System.out.println(readUnsignedIntLittleEndian(data, index));
    }/*from  ww  w  . j av a 2 s. co m*/

    /**
     * Read 4 bytes in Little-endian byte order.
     * 
     * @param data, the original byte array
     * @param index, start to read from.
     * @return
     */
    public static long readUnsignedIntLittleEndian(byte[] data, int index) {
        long result = (long) (data[index] & 0xFF)
                | (long) ((data[index + 1] & 0xFF) << 8)
                | (long) ((data[index + 2] & 0xFF) << 16)
                | (long) ((data[index + 3] & 0xFF) << 24);
        return result;
    }
}

Related Tutorials