Subversion Repositories programming

Rev

Rev 209 | Rev 213 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
210 ira 1
/*******************************************************************************
2
 * File: P2_EncDec.java
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
4
 * Class: CS380 - Computer Networking
5
 *
6
 * Assignment: Project #2
7
 * Date Last Modified: 2006-02-04
8
 *
9
 * Purpose: Encode / Decode a number to / from 4B5B Encoding.
10
 *
11
 ******************************************************************************/
12
 
209 ira 13
import java.io.*;
14
import java.util.*;
15
 
16
public class P2_EncDec
17
{
18
    /*
210 ira 19
    private static final byte[][] encodingTable = {
209 ira 20
        { 0x0, 0x1E },
21
        { 0x1, 0x09 },
22
        { 0x2, 0x14 },
23
        { 0x3, 0x15 },
24
        { 0x4, 0x0A },
25
        { 0x5, 0x0B },
26
        { 0x6, 0x0E },
27
        { 0x7, 0x0F },
28
        { 0x8, 0x12 },
29
        { 0x9, 0x13 },
30
        { 0xA, 0x16 },
31
        { 0xB, 0x17 },
32
        { 0xC, 0x1A },
33
        { 0xD, 0x1B },
34
        { 0xE, 0x1C },
35
        { 0xF, 0x1D }};
36
    */
37
 
38
    public static final String[][] encodingTable = {
39
        { "0000", "11110" },
40
        { "0001", "01001" },
41
        { "0010", "10100" },
42
        { "0011", "10101" },
43
        { "0100", "01010" },
44
        { "0101", "01011" },
45
        { "0110", "01110" },
46
        { "0111", "01111" },
47
        { "1000", "10010" },
48
        { "1001", "10011" },
49
        { "1010", "10110" },
50
        { "1011", "10111" },
51
        { "1100", "11010" },
52
        { "1101", "11011" },
53
        { "1110", "11100" },
54
        { "1111", "11101" }};
55
 
56
 
210 ira 57
    /**
58
     * Method: convert4B()
59
     * Purpose: Convert a number from it's 4B representation to the
60
     * corresponding 5B representation.
61
     */
209 ira 62
    public static String convert4B (String _4BNum)
63
    {
64
        int i = encodingTable.length - 1;
65
 
66
        for (; i >= 0; i--)
67
            if (encodingTable[i][0].equals(_4BNum))
68
                return encodingTable[i][1];
69
 
70
        throw new IllegalArgumentException();
71
    }
72
 
210 ira 73
    /**
74
     * Method: convert5B()
75
     * Purpose: Convert a number from it's 5B representation to the
76
     * corresponding 4B representation.
77
     */
209 ira 78
    public static String convert5B (String _5BNum)
79
    {
80
        int i = encodingTable.length - 1;
81
 
82
        for (; i>=0; i--)
83
            if (encodingTable[i][1].equals(_5BNum))
84
                return encodingTable[i][0];
85
 
86
        throw new IllegalArgumentException();
87
    }
88
}
89