Mod10计算? [英] Mod10 Calculation?

查看:731
本文介绍了Mod10计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来尝试生成用于生成MOD10校验位的代码段.  从理论上讲,这很简单,但是由于我是一个全新的人,所以我不确定如何将其转换为C#.

I need help trying to come up with a snippet for generating a MOD10 check digit.  In theory it's pretty simple, but I'm not sure how to translate this into C# as I am completely new.

如果我有8位数字,例如12345678,我需要将偶数位置乘以1,将奇数位置乘以2. :

If I have a 8 digit number, say 12345678, I need to multiple the even positions by 1 and the odd by 2.  Then I need to take those results and add the digits together, which would break down like this:

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8

1 2 1 2 1 2 1 2

1 2 1 2 1 2 1 2

--------------------

--------------------

1 + 4 + 3 + 8 + 5 + 1 + 2 + 7 + 1 + 6(请注意,如果您获得两位数的结果,则必须分别添加两位数)

1+4+3+8+5+1+2+7+1+6 (Keep in mind if you get a double digit result you have to add each digit separately)

我从中得到38,如果您得到38%10,您得出8,而10-8是2,则校验位将为2. 如何将其转换为C#?

I get 38 from that, and if you do 38 % 10 you come up with 8, and 10-8 is 2 therefore the check digit would be 2.  How can I translate this into C#?

推荐答案

嗯...显示的步骤是将偶数位置乘以 2 和奇数 1 ".哪一个?

Humm... the steps you shown are "multiply the even positions by 2 and the odd by 1". Which one?

将偶数位置乘以1,将奇数位置乘以2"

With "multiply the even positions by 1 and the odd by 2"

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8

2 1 2 1 2 1 2 1

2 1 2 1 2 1 2 1

2 2 6 4106148

2 2 6 4106148

2 + 2 + 6 + 4 + 1 + 0 + 6 + 1 + 4 + 8 = 34

2 + 2 + 6 + 4 + 1 + 0 + 6 + 1 + 4 + 8 = 34

34 mod 10 = 4

34 mod 10 = 4

应该是这样的:

            string a = "12345678";
            List<char> result = new List<char>();
            for (int i = 0; i < a.Length; i++)
            {
                result.AddRange(Convert.ToString((Convert.ToInt32(a[i]) - 48) * (i % 2 == 1 ? 1 : 2 )));
            }
            Console.WriteLine(10 - result.Select(x => Convert.ToInt32(x)).Sum() % 10);


这篇关于Mod10计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆