Java Binary Encode toBinFromOct(final String octSymbols)

Here you can find the source of toBinFromOct(final String octSymbols)

Description

Transform a string of octal symbols to a string of binary symbols

License

Open Source License

Parameter

Parameter Description
octSymbols The oct symbol string to transform

Return

The binary string that corresponds to the input octal string

Declaration

public static String toBinFromOct(final String octSymbols) 

Method Source Code

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

public class Main {
    public final static int BITS_PER_OCT_DIGIT = 3;

    /**// ww  w  .ja va 2  s .  c  om
     * Transform a string of octal symbols to a string of binary symbols
     *
     * @param octSymbols
     *            The oct symbol string to transform
     * @return The binary string that corresponds to the input octal string
     */
    public static String toBinFromOct(final String octSymbols) {
        StringBuilder bits = new StringBuilder(octSymbols.length() * BITS_PER_OCT_DIGIT);
        for (int i = 0; i < octSymbols.length(); i++) {
            bits.append(toBinFromOctChar(octSymbols.charAt(i)));
        }

        return (bits.toString());
    }

    public static String toBinFromOctChar(final char oct) {
        switch (oct) {
        case '0':
            return ("000");
        case '1':
            return ("001");
        case '2':
            return ("010");
        case '3':
            return ("011");
        case '4':
            return ("100");
        case '5':
            return ("101");
        case '6':
            return ("110");
        case '7':
            return ("111");
        default:
            throw new IllegalArgumentException(
                    "The input character \'" + oct + "\'is not a valid octal character.");
        }
    }
}

Related

  1. toBinaryStringAddress(long address)
  2. toBinaryText(StringBuffer buf)
  3. toBinFromByte(final byte b)
  4. toBinFromHex(final String hexSymbols)
  5. toBinFromHexChar(final char hex)
  6. toBinFromOctChar(final char oct)
  7. toBinString(byte b)
  8. toBinString(byte value)
  9. toBinString(byte[] byteArray)