C # generate modulo 10 test bit

Source: Internet
Author: User

The following excerpt from the Wikipedia "http://zh.wikipedia.org/wiki/Luhn%E7%AE%97%E6%B3%95" Luhn algorithm (Luhn algorithm), also known as the "modulo" (Mod 10) algorithm, is a simple checksum algorithm, generally used to verify the identification code, such as the issuing bank identification code, International mobile Device Identification Code (IMEI), the United States to provide a trademark identification number, or Canada Social Security number. This algorithm is now in the public domain and has been widely used, it is not a secure cryptographic hash function, it is designed to prevent accidental errors rather than malicious attacks. Description The Luhn algorithm validates a string of numbers with a checksum, which is usually appended to the end of the string to obtain a complete identification number. We take the number "7992739871" as an example to calculate its check digit: 1.starting from the check digit, from right to left, and even digit by 2 (for example, 7*2=14), the No. 0 bit is also counted as an even digit, and then the two digit digit is added to the 10-bit value (for example, 10:1+0=1,14:1+4=5);2. Add the resulting figures (in this case, in the example);3. The number and modulus 10 (in this case 7), and then 10 de minus (in this case, 3), to obtain a check digit. 4. If the check bit is 10, then take 0 (actually the number and is 10; searched the internet for a lap and found no one mentioned it)
Original number 7 9 9 2 7 3 9 8 7 1 X
Even digit multiply by 2 7 18 9 4 7 6 9 16 7 2 X
Add a number 7 9 9 4 7 6 9 7 7 2 =67

5. Implementing the Code

public static int Getluhn (String str)

{

int n = 0;

int ilen = str. Length;

for (int j = Ilen; j > 0; j--)

{

if ((ilen-j)% 2 = = 0)//even digit

{

int snum = Int. Parse (Str[j-1]. ToString ()) * 2;//even digits multiplied by 2

10-bit value + single digit value

string s = snum.tostring ();

int s1 = 0;

if (s.length = = 2)

S1 = Int. Parse (S[0]. ToString ()) + Int. Parse (S[1]. ToString ());

Else

S1 = Int. Parse (S[0]. ToString ());

n = n + s1;

}

Else

{

n = n + int. Parse (Str[j-1]. ToString ());

}

}

n = ten-(n% 10);

if (n = = 10)

n = 0;

return n;

}

C # generate modulo 10 test bit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.