Android Open Source - TextEncoder Octal 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;
/* w w w  .ja  va2s  . c  om*/
import android.content.Context;

public class OctalMessage extends Message {

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

    public OctalMessage(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]);
        }
        tempArray = calcDecimalArray(tempArray);
        setLongValuesArray(tempArray);
    }

    public OctalMessage(long[] inputArray) throws NumberFormatException {
        super(inputArray);
    }

    private long calcOctalValue(long value) {
        String result = "";
        while (value > 0) {
            long rem = value % 8;
            value /= 8;
            result = rem + result;
        }
        return Long.parseLong(result);
    }

    private long[] calcDecimalArray(long[] array) {
        for (int i = 0; i < array.length; i++) {
            String[] digits = String.valueOf(array[i]).split("(?!^)");
            long result = 0;
            for (int j = 0; j < digits.length; j++) {
                result +=  Long.parseLong(digits[j]) * Math.pow(8, digits.length-j-1);
            }
            array[i] = result;
        }
        return array;
    }

    @Override
    public String toString() {
        String result = "";
        for (int i = 0; i < getLongValuesArray().length; i++) {
            result += calcOctalValue(getLongValuesArray()[i]);
            if (i != getLongValuesArray().length - 1)
                result += " ";
        }
        return result;
    }

    private boolean checkValid(String[] array) throws NumberFormatException {
        for (String s: array) {
            if (!s.matches("^[0-7]+$"))
                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