Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Converts String to boolean array
     * @param data
     * @param charsetSize
     * @return
     */
    public static boolean[] data2Binary(String data, int charsetSize) {
        boolean[] bits = new boolean[data.length() * charsetSize];

        int index = 0;
        for (char c : data.toCharArray()) {
            int asciiVal = (int) c;
            System.out.print(c + ":" + asciiVal + " ");

            asciiVal <<= 1; // throw away most left bit. (is always a 0, cfr. ASCII-table)

            for (int i = 0; i < charsetSize; i++) {
                bits[index] = (asciiVal & (int) Math.pow(2, charsetSize)) == 0 ? false : true;
                System.out.print(bits[index] + " ");
                asciiVal <<= 1; // throw away left most bit
                index++;
            }
            System.out.println();
        }

        return bits;
    }
}