Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static Short byteArrayToShort(byte[] bytes) {
        if (bytes.length != (Short.SIZE / Byte.SIZE)) {
            throw new IllegalArgumentException("Incorrect array size to convert to a short");
        }
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        buffer.order(ByteOrder.BIG_ENDIAN);
        return buffer.getShort();
    }
}