用Java编码代码

发布于 2024-08-11 11:59:24 字数 499 浏览 8 评论 0原文

在过去的几周里,我读完了这本书错误控制编码:基础知识和应用程序 为了了解电信公司初级编程角色的 BCH(Bose、Chaudhuri、Hocquenghem)代码。

这本书主要涵盖了该主题背后的数学和理论,但我正在努力实现一些概念;主要是获取下一个 n 代码字。我有一个 GUI(通过 NetBeans 实现,因此我不会发布代码,因为文件很大),它传递代码以获取下一个 n 个数字:

生成这些数字是我遇到问题的地方。如果我可以仅在编码方法中完成所有这些,而不是使用 GUI 循环,我的生活将会轻松十倍。

这已经让我发疯了好几天了,因为从输入生成 0000000000 很容易,但我不知道从那里用我的代码去哪里。然后我该怎么做才能生成下一个工作号码?

任何有关生成上述代码的帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-08-18 11:59:24

(大编辑......)多玩一点代码,这似乎有效:

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");
    }
}

我更喜欢抛出异常而不是返回特殊字符串。在这种情况下,我忽略了异常,通常我会说这是不好的做法,但对于这种情况,我认为这就是你想要的。

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

夏九 2024-08-18 11:59:24

很难说,如果我遇到你的问题,但在多次阅读你的问题后,也许这就是你正在寻找的:

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;
}

解决方案中有一个缺陷,toOctalString 结果不是 0 填充的。如果这就是您想要的,我建议在编码调用中使用 String.format("", i)

更新

要在当前调用中使用它,请将对encoding(String str) 的调用替换为对此方法的调用。您将收到包含所有编码的有序列表。

我假设,您只对八进制值感兴趣 - 我的错误,现在我认为您只是忘记了示例中值 000009 的编码,从而删除了不相关的八进制内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文