Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.nio.ByteOrder;

public class Main {
    public static byte[] Shorts2Bytes(short[] datas) {
        byte bLength = 2;
        byte[] buf = new byte[datas.length * bLength];
        for (int iLoop = 0; iLoop < datas.length; iLoop++) {
            byte[] temp = getBytes(datas[iLoop]);
            for (int jLoop = 0; jLoop < bLength; jLoop++) {
                buf[iLoop * bLength + jLoop] = temp[jLoop];
            }
        }
        return buf;
    }

    private static byte[] getBytes(short data) {
        return getBytes(data, testCPU());
    }

    private static byte[] getBytes(int data, boolean bBigEnding) {
        byte[] buf = new byte[4];
        if (bBigEnding) {
            for (int i = buf.length - 1; i >= 0; i--) {
                buf[i] = (byte) (data & 0x000000ff);
                data >>= 8;
            }
        } else {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (data & 0x000000ff);
                data >>= 8;
            }
        }
        return buf;
    }

    public static boolean testCPU() {
        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
            return true;
        } else {
            return false;
        }
    }
}