byte array to Short - Java java.lang

Java examples for java.lang:byte Array to short

Description

byte array to Short

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] in = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(toShort(in));
    }/*from w  w  w .  j  a  va 2s . c  om*/

    public static short toShort(byte[] in) {
        short out = 0;
        for (int i = in.length - 1; i > 0; i--) {
            out |= in[i] & 0xff;
            out <<= 8;
        }
        out |= in[0] & 0xff;
        return out;
    }
}

Related Tutorials