Subversion Repositories programming

Rev

Rev 210 | Rev 215 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * File: P2_EncDec.java
 * Author: Ira W. Snyder (devel@irasnyder.com)
 * Class: CS380 - Computer Networking
 *
 * Assignment: Project #2
 * Date Last Modified: 2006-02-04
 *
 * Purpose: Encode / Decode a number to / from 4B5B Encoding.
 *
 ******************************************************************************/

import java.io.*;
import java.util.*;

public class P2_EncDec
{
    public static final String[][] encodingTable = {
        { "0000", "11110" },
        { "0001", "01001" },
        { "0010", "10100" },
        { "0011", "10101" },
        { "0100", "01010" },
        { "0101", "01011" },
        { "0110", "01110" },
        { "0111", "01111" },
        { "1000", "10010" },
        { "1001", "10011" },
        { "1010", "10110" },
        { "1011", "10111" },
        { "1100", "11010" },
        { "1101", "11011" },
        { "1110", "11100" },
        { "1111", "11101" }};


    /**
     * Method: convert4B()
     * Purpose: Convert a number from it's 4B representation to the
     * corresponding 5B representation.
     */
    public static String convert4B (String _4BNum)
    {
        int i = encodingTable.length - 1;

        for (; i >= 0; i--)
            if (encodingTable[i][0].equals(_4BNum))
                return encodingTable[i][1];

        throw new IllegalArgumentException();
    }

    /**
     * Method: convert5B()
     * Purpose: Convert a number from it's 5B representation to the
     * corresponding 4B representation.
     */
    public static String convert5B (String _5BNum)
    {
        int i = encodingTable.length - 1;

        for (; i>=0; i--)
            if (encodingTable[i][1].equals(_5BNum))
                return encodingTable[i][0];

        throw new IllegalArgumentException();
    }
}