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

Java examples for Internationalization:Big Endian Little Endian

Description

Reads a Big Endian WORD 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(readWordBigEndian(data, offset));
    }/*  w  w w.  j av  a 2 s. com*/

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

Related Tutorials