get Short Value From Byte Array Big Endian - Java Internationalization

Java examples for Internationalization:Charset

Description

get Short 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(getShortValueFromByteArrayBigEndian(byteArray,
                offset));/*from   w w  w. ja v  a 2  s .c  o m*/
    }

    public static short getShortValueFromByteArrayBigEndian(
            byte[] byteArray, int offset) {
        short val = 0;
        val = (short) (((byteArray[offset + 1] << 8 >> 8) & 0xFF) + ((byteArray[offset] << 8) & 0xFF00));
        return val;
    }
}

Related Tutorials