Rev 209 | 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{/*private static final byte[][] encodingTable = {{ 0x0, 0x1E },{ 0x1, 0x09 },{ 0x2, 0x14 },{ 0x3, 0x15 },{ 0x4, 0x0A },{ 0x5, 0x0B },{ 0x6, 0x0E },{ 0x7, 0x0F },{ 0x8, 0x12 },{ 0x9, 0x13 },{ 0xA, 0x16 },{ 0xB, 0x17 },{ 0xC, 0x1A },{ 0xD, 0x1B },{ 0xE, 0x1C },{ 0xF, 0x1D }};*/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();}}