Android Open Source - TextEncoder Binary Message






From Project

Back to project page TextEncoder.

License

The source code is released under:

Apache License

If you think the Android project TextEncoder listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.csab.TextEncoder;
/*from  w ww.jav a 2 s . c om*/
import android.content.Context;

public class BinaryMessage extends Message {

    private static final int NUMBER_OF_BITS = 8;

    public BinaryMessage(byte[] inputArray) {
        super(inputArray);
    }

    public BinaryMessage(String inputString, Context context) throws NumberFormatException {
        super(inputString, context);
        checkValid(getStringValuesArray());
        long[] tempArray = new long[getStringValuesArray().length];
        for (int i = 0; i < tempArray.length; i++) {
            tempArray[i] = Long.parseLong(getStringValuesArray()[i], 2);
        }
        setLongValuesArray(tempArray);
    }

    public BinaryMessage(long[] inputArray) {
        super(inputArray);
    }

    @Override
    public String toString() {
        String[] binaryStringArray = new String[getLongValuesArray().length];
        String result = "";
        for (int i = 0; i < getLongValuesArray().length; i++) {
            binaryStringArray[i] = Long.toBinaryString(getLongValuesArray()[i]);
            while (binaryStringArray[i].length() < NUMBER_OF_BITS) {
                binaryStringArray[i] = "0" + binaryStringArray[i];
            }
            if (i == getLongValuesArray().length - 1) {
                result += binaryStringArray[i];
            } else {
                result += binaryStringArray[i] + " ";
            }
        }
        return result;
    }

    private boolean checkValid(String[] array) throws NumberFormatException {
        for (String s : array) {
            if (!s.matches("^[01]+$")) {
                throw new NumberFormatException(
                        getContext().getResources().getString(R.string.invalid_input_message));
            }
        }
        return true;
    }

}




Java Source Code List

com.csab.TextEncoder.AsciiMessage.java
com.csab.TextEncoder.Base64Message.java
com.csab.TextEncoder.BinaryMessage.java
com.csab.TextEncoder.Converter.java
com.csab.TextEncoder.DecimalMessage.java
com.csab.TextEncoder.HexMessage.java
com.csab.TextEncoder.MainActivity.java
com.csab.TextEncoder.Message.java
com.csab.TextEncoder.OctalMessage.java