Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**
     * Size of a UTF-8 character in bits.
     */
    public static final int UTF8_SIZE = 8;

    /**
     * Converts a string to his binary equivalent.
     *
     * @param text The string that must be converted, cannot be null
     * @return An array of integers representing the binary equivalent
     */
    public static int[] getBinarySequence(String text) {

        if (text == null) {
            throw new NullPointerException("Given text cannot be null");
        }

        char[] textArray = text.toCharArray();
        int[] bitList = new int[textArray.length * UTF8_SIZE];
        int bitListPtr = 0;

        for (char asciiValue : textArray) {

            for (int bitWise = UTF8_SIZE - 1; bitWise >= 0; bitWise--) {
                int tempBit = ((asciiValue & (1 << bitWise)) > 0) ? 1 : 0;

                bitList[bitListPtr] = tempBit;
                bitListPtr++;
            }
        }

        return bitList;
    }
}