Search code examples
javaerror-correction

Encoding codes in Java


Over the past couple of weeks I've read through the book Error Control Coding: Fundamentals and Applications in order to learn about BCH (Bose, Chaudhuri, Hocquenghem) Codes for an junior programming role at a telecoms company.

This book mostly covers the mathematics and theory behind the subject, but I'm struggling to implement some of the concepts; primarily getting the next n codewords.I have a GUI (implemented through NetBeans, so I won't post the code as the file is huge) that passes a code in order to get the next n numbers:

Generating these numbers is where I am having problems. If I could go through all of these within just the encoding method instead of looping through using the GUI my life would be ten times easier.

This has been driving me crazy for days now as it is easy enough to generate 0000000000 from the input, but I am lost as to where to go from there with my code. What do I then do to generate the next working number?

Any help with generating the above code would be appreciated.


Solution

  • (big edit...) Playing with the code a bit more this seems to work:

    import java.util.ArrayList;
    import java.util.List;
    
    
    public class Main
    {
        public static void main(final String[] argv)
        {
            final int startValue;
            final int iterations;
            final List<String> list;
    
            startValue = Integer.parseInt(argv[0]);
            iterations = Integer.parseInt(argv[1]);
            list = encodeAll(startValue, iterations);
            System.out.println(list);
        }
    
        private static List<String> encodeAll(final int startValue, final int iterations)
        {
            final List<String> allEncodings;
    
            allEncodings = new ArrayList<String>();
    
            for(int i = 0; i < iterations; i++)
            {
                try
                {
                    final int    value;
                    final String str;
                    final String encoding;
    
                    value = i + startValue;
                    str = String.format("%06d", value);
                    encoding = encoding(str);
                    allEncodings.add(encoding);
                }
                catch(final BadNumberException ex)
                {
                    // do nothing
                }
            }
    
            return allEncodings;
        }
    
        public static String encoding(String str)
            throws BadNumberException
        {
            final int[]         digit;
            final StringBuilder s;
    
            digit = new int[10];
    
            for(int i = 0; i < 6; i++)
            {
                digit[i] = Integer.parseInt(String.valueOf(str.charAt(i)));
            }
    
            digit[6] = ((4*digit[0])+(10*digit[1])+(9*digit[2])+(2*digit[3])+(digit[4])+(7*digit[5])) % 11;
            digit[7] = ((7*digit[0])+(8*digit[1])+(7*digit[2])+(digit[3])+(9*digit[4])+(6*digit[5])) % 11;
            digit[8] = ((9*digit[0])+(digit[1])+(7*digit[2])+(8*digit[3])+(7*digit[4])+(7*digit[5])) % 11;
            digit[9] = ((digit[0])+(2*digit[1])+(9*digit[2])+(10*digit[3])+(4*digit[4])+(digit[5])) % 11;
    
            // Insert Parity Checking method (Vandermonde Matrix)
            s = new StringBuilder();
    
            for(int i = 0; i < 9; i++)
            {
                s.append(Integer.toString(digit[i]));
            }
    
            if(digit[6] == 10 || digit[7] == 10 || digit[8] == 10 || digit[9] == 10)
            {
                throw new BadNumberException(str);
            }
    
            return (s.toString());
        }
    }
    
    class BadNumberException
        extends Exception
    {
        public BadNumberException(final String str)
        {
            super(str + " cannot be encoded");
        }
    }
    

    I prefer throwing the exception rather than returning a special string. In this case I ignore the exception which normally I would say is bad practice, but for this case I think it is what you want.