使用c#使用子集C找到模数103的校验位 [英] using c# to find check digit for modulus 103 using subset C

查看:148
本文介绍了使用c#使用子集C找到模数103的校验位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下......我得到输出我不认为它是正确的..在这段代码中它只适用于24位数字。如果位数变化我不能重复使用这个代码我发现在下面的实现中的逻辑For循环一次取两位数并迭代循环。请帮助我..提前付款

代码:

my code is as follows..thought i got the output i don't think its correct..in this code it is only meant for 24 digit number.if the number of digits vary i cannot reuse this code i am finding a logic to implement in this following For loop to take two digits at a time and iterate the loop.kindly help me..Thanx in advance
Code:

const int C_Code = 105;
           string barCode;
           int d, a = 1, sum = 0, product,num;
           int checkSum = 0, checkDigit,x;
           Console.WriteLine("Entre barcode :");
           barCode = System.Console.ReadLine();
           num = (barCode.Length) / 2;
               for (int i = 0; i<=num+10 ; i=i+2)
           {
               d = Convert.ToInt32(barCode.Substring(i, 2));
               product = a * d;
               sum += product;
               a++;
           }

           sum += C_Code;
           Console.WriteLine(sum);
           checkSum = sum /103;
           Console.WriteLine(checkSum);
           x = checkSum * 103;
           Console.WriteLine(x);
           checkDigit = sum - x;
           Console.WriteLine(checkDigit);
           Console.ReadLine();

推荐答案

11月。 2013年更新了包含Code128 ABC条形码生成的条形码的CodeProject文章:[ ^ ]。您可以研究他的Code128.cs文件,看看他如何处理不同类型的Code128条形码字符串。试试这个:
Nov. 2013 updated CodeProject article on barcodes that includes Code128 A-B-C Bar-code generation: [^]. You can study his Code128.cs file to see how he handles the different types of Code128 barcode strings. Try this:
private void testBarCode128CRead(string bCodeStr)
{
    const int C_Code = 105;

    int checkSum, checkDigit;
    int sum = C_Code;
    int weight = 1;

    // check length of string parameter is even number ?
    if ((bCodeStr.Length % 2) != 0) throw new ArgumentException("input string not even");

    Console.WriteLine("input string length: " + bCodeStr.Length);

    for (int i = 0; i < bCodeStr.Length; i += 2)
    {
        sum += weight++ * Convert.ToInt32(bCodeStr.Substring(i, 2));
    }

    checkSum = sum / 103;
    checkDigit = sum % 103;

    Console.WriteLine("sum {0} checkSum {1} checkDigit {2}", sum, checkSum, checkDigit);
}

请注意,我只是在这里关注您的代码;如果你的计算不正确,那么这个代码也是如此。



问题:



1.是什么让你认为你的计算不正确?您是否进行了特定的测试,结果不正确:如果是,请显示测试输入数据。



2.是否要求输入字符串长度为24个字符。如果没有,你将如何处理不长或更长的输入?你在这里按字符对处理输入数据?

Note that I'm just following your code here; if your calculations are incorrect, so is this code.

Questions:

1. What makes you think your calculation is not correct ? Is there a specific test you've done that gave an incorrect result: if so, please show the test input data.

2. Is it a requirement that the input string be 24 characters long. If not, how would you handle input which is not as long, or longer ? Process the input data by character-pairs as you are doing here ?


const int C_Code = 105;

string barCode;

int d,a = 1,sum = 0,product;

int checkSum = 0,checkDigit,x;



Console.WriteLine( Entre条形码);

barCode = System.Console.ReadLine();



for(int i = 0; i< = (barCode.Length)-2; i = i + 2)

{

d = Convert.ToInt32(barCode.Substring(i,2));

product = a * d;

sum + = product;

a ++;

}



sum + = C_Code;

Console.WriteLine(sum);

checkSum = sum / 103;

Console.WriteLine(checkSum);

x = checkSum * 103;

Console.WriteLine(x);

checkDigit = sum - x;

Console.WriteLine(checkDigit);
const int C_Code = 105;
string barCode;
int d, a = 1, sum = 0, product;
int checkSum = 0, checkDigit,x;

Console.WriteLine("Entre barcode");
barCode = System.Console.ReadLine();

for (int i = 0; i<=(barCode.Length)-2; i=i+2)
{
d = Convert.ToInt32(barCode.Substring(i, 2));
product = a * d;
sum += product;
a++;
}

sum += C_Code;
Console.WriteLine(sum);
checkSum = sum /103;
Console.WriteLine(checkSum);
x = checkSum * 103;
Console.WriteLine(x);
checkDigit = sum - x;
Console.WriteLine(checkDigit);


这篇关于使用c#使用子集C找到模数103的校验位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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