Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static int twoBytesToUnsignedInt(byte one, byte two) {
        return (int) eightBytesToUnsignedLong((byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, one, two);
    }

    public static long eightBytesToUnsignedLong(byte zero, byte one, byte two, byte three, byte four, byte five,
            byte six, byte seven) {
        return byteArrayToUnsignedLong(new byte[] { zero, one, two, three, four, five, six, seven });
    }

    /**
     * @param byteArray
     * @return
     */
    public static long byteArrayToUnsignedLong(byte[] byteArray) {
        int length;
        long value = 0;
        if (byteArray.length == Byte.SIZE / Byte.SIZE) {
            length = Byte.SIZE / Byte.SIZE;
        } else if (byteArray.length == Short.SIZE / Byte.SIZE) {
            length = Short.SIZE / Byte.SIZE;
        } else if (byteArray.length == Integer.SIZE / Byte.SIZE) {
            length = Integer.SIZE / Byte.SIZE;
        } else if (byteArray.length == Long.SIZE / Byte.SIZE) {
            length = Long.SIZE / Byte.SIZE;
        } else
            throw new IllegalArgumentException("Array length must match one of the following types:\n Byte=="
                    + Byte.SIZE + ", Short==" + Short.SIZE + ", Integer==" + Integer.SIZE + ", Long==" + Long.SIZE);

        for (int i = 0; i < length; i++) {
            value |= ((0xffL & byteArray[i]) << (8 * (length - i - 1)));
        }
        return value;
    }
}