Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayOutputStream;

public class Main {
    public static char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    public static void printIntArray(int[] intArray, int lineSize) {
        for (int i = 0; i < intArray.length; i++) {
            byte[] byteArray = toByteArray(intArray[i]);
            int j = 0;
            while (j < byteArray.length) {
                System.out.print(chars[(byteArray[j] & 0xFF) >> 4]);
                System.out.print(chars[byteArray[j] & 0xF]);
                j++;
            }
            System.out.print(" ");
            if (i % lineSize == (lineSize - 1)) {
                System.out.println(" ");
            }
        }
    }

    public static byte[] toByteArray(int a, int b, int c, int d, int e, int f, int g, int h) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(32);
        baos.write(toByteArray(a));
        baos.write(toByteArray(b));
        baos.write(toByteArray(c));
        baos.write(toByteArray(d));
        baos.write(toByteArray(e));
        baos.write(toByteArray(f));
        baos.write(toByteArray(g));
        baos.write(toByteArray(h));
        return baos.toByteArray();
    }

    public static byte[] toByteArray(int i) {
        byte[] byteArray = new byte[4];
        byteArray[0] = (byte) (i >>> 24);
        byteArray[1] = (byte) ((i & 0xFFFFFF) >>> 16);
        byteArray[2] = (byte) ((i & 0xFFFF) >>> 8);
        byteArray[3] = (byte) (i & 0xFF);
        return byteArray;
    }
}