Subversion Repositories programming

Rev

Rev 209 | Rev 213 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 209 Rev 210
Line -... Line 1...
-
 
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
 
1
import java.io.*;
13
import java.io.*;
2
import java.util.*;
14
import java.util.*;
3
 
15
 
4
public class P2_EncDec
16
public class P2_EncDec
5
{
17
{
6
    /*
18
    /*
7
    private final byte[][] encodingTable = {
19
    private static final byte[][] encodingTable = {
8
        { 0x0, 0x1E },
20
        { 0x0, 0x1E },
9
        { 0x1, 0x09 },
21
        { 0x1, 0x09 },
10
        { 0x2, 0x14 },
22
        { 0x2, 0x14 },
11
        { 0x3, 0x15 },
23
        { 0x3, 0x15 },
12
        { 0x4, 0x0A },
24
        { 0x4, 0x0A },
Line 40... Line 52...
40
        { "1101", "11011" },
52
        { "1101", "11011" },
41
        { "1110", "11100" },
53
        { "1110", "11100" },
42
        { "1111", "11101" }};
54
        { "1111", "11101" }};
43
 
55
 
44
 
56
 
-
 
57
    /**
-
 
58
     * Method: convert4B()
-
 
59
     * Purpose: Convert a number from it's 4B representation to the
-
 
60
     * corresponding 5B representation.
-
 
61
     */
45
    public static String convert4B (String _4BNum)
62
    public static String convert4B (String _4BNum)
46
    {
63
    {
47
        int i = encodingTable.length - 1;
64
        int i = encodingTable.length - 1;
48
 
65
 
49
        for (; i >= 0; i--)
66
        for (; i >= 0; i--)
Line 51... Line 68...
51
                return encodingTable[i][1];
68
                return encodingTable[i][1];
52
 
69
 
53
        throw new IllegalArgumentException();
70
        throw new IllegalArgumentException();
54
    }
71
    }
55
 
72
 
-
 
73
    /**
-
 
74
     * Method: convert5B()
-
 
75
     * Purpose: Convert a number from it's 5B representation to the
-
 
76
     * corresponding 4B representation.
-
 
77
     */
56
    public static String convert5B (String _5BNum)
78
    public static String convert5B (String _5BNum)
57
    {
79
    {
58
        int i = encodingTable.length - 1;
80
        int i = encodingTable.length - 1;
59
 
81
 
60
        for (; i>=0; i--)
82
        for (; i>=0; i--)
61
            if (encodingTable[i][1].equals(_5BNum))
83
            if (encodingTable[i][1].equals(_5BNum))
62
                return encodingTable[i][0];
84
                return encodingTable[i][0];
63
 
85
 
64
        throw new IllegalArgumentException();
86
        throw new IllegalArgumentException();
65
    }
87
    }
66
 
-
 
67
    public static void main (String[] args) throws Exception
-
 
68
    {
-
 
69
        String conv4 = "1000";
-
 
70
        String conv5 = "11100";
-
 
71
 
-
 
72
        System.out.printf ("Converting %s -> %s\n", conv4, convert4B (conv4));
-
 
73
        System.out.printf ("Converting %s -> %s\n", conv5, convert5B (conv5));
-
 
74
    }
-
 
75
 
-
 
76
}
88
}
77
 
89