views:

230

answers:

2

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.

+1  A: 

Hard to tell, if I got your problem, but after reading your question several times, maybe that's what you're looking for:

public List<String> encodeAll() {
  List<String> allEncodings = new ArrayList<String>();
  for (int i = 0; i < 1000000 ; i++) { 
    String encoding = encoding(Integer.toString(i));
    allEncodings.add(encoding);
  }
  return allEncodings;
}

There's one flaw in the solution, the toOctalString results are not 0-padded. If that's what you want, I suggest using String.format("<something>", i) in the encoding call.

Update

To use it in your current call, replace a call to encoding(String str) with call to this method. You'll receive an ordered List with all encodings.

I aasumed, you were only interested in octal values - my mistake, now I think you just forgot the encoding for value 000009 in you example and thus removed the irretating octal stuff.

Andreas_D
How would I use this within my current code? I cannot see where I would pass in 000000 to get the usable numbers.
AlexT
+1  A: 

(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.

TofuBeer
This works perfectly for my example, but I would want to generate n codewords based on any input, not just 000000. I am using a GUI to enter 000000 and I would like to enter any six digit number and encode it. Is this possible and would it require any drastic changes?
AlexT
THe updated code should do what you want. Now you would run "java Main 111111 10" and that would start it at 111111 and go for the next 10 numbers. In your GUI you would get the start value and the number of iterations and convert them with the Integer.parseInt method (it will throw a NumberFormatException if the string is not an int) and then pass those methods to the encodeAll method. I'd also probably change encodeALl to encodeRange or something like that.
TofuBeer
Works perfect, thank you!
AlexT