Bar code check bit calculation formula PHP, T-SQL program

Source: Internet
Author: User
Recently, we have been involved in the batch modification of the 13th-bit verification bits for bar code calculation. AI, bar code printers, bar code scanners, and other software have built-in verification bits generated. We have also found a bar code generator for online search, but it is not flexible enough for us, write a small program to calculate the check bit OK first look at the PHP version, with the online... syntaxHighlighter. all ();

Recently, we have been involved in the batch modification of the 13th-bit verification bits for bar code calculation. AI, bar code printers, bar code scanners, and other software have built-in verification bits generated. We have also found a bar code generator for online search, but it is not flexible enough for us, write a small program to calculate the check bit

Okay. let's take a look at the PHP version first. it's a lot easier to find on the Internet.


Function en13 ($ code)
{
$ Tmp1 = 0;
$ Tmp2 = 0;
For ($ I = 0; $ I <12; $ I ++)
{
If ($ I % 2 = 0)
$ Tmp1 + = substr ($ code, $ I, 1 );
Else
$ Tmp2 + = substr ($ code, $ I, 1 );
}
Return (10-($ tmp2 * 3 + $ tmp1) % 10) % 10;
}
 
// Test www.2cto.com
$ T = "6939762911740 ";
Echo en13 ($ t );
Function en13 ($ code)
{
$ Tmp1 = 0;
$ Tmp2 = 0;
For ($ I = 0; $ I <12; $ I ++)
{
If ($ I % 2 = 0)
$ Tmp1 + = substr ($ code, $ I, 1 );
Else
$ Tmp2 + = substr ($ code, $ I, 1 );
}
Return (10-($ tmp2 * 3 + $ tmp1) % 10) % 10;
}

// Test
$ T = "6939762911740 ";
Echo en13 ($ t );
In addition, if you want to update the data in the MSSQL database in batches, you do not need to write programs. Instead, you can directly update the data in the SQL Enterprise Manager.

T-SQL code


Declare @ str as varchar (15)
Declare @ tmp as varchar (30)
Declare @ t1 as int
Declare @ t2 as int
Declare @ I int
Set @ t1 = 0
Set @ t2 = 0
Set @ str = '20140901'
Set @ I = 1
While @ I <= 12
Begin
If @ I % 2 = 0
Begin
Set @ t1 = @ t1 + substring (@ str, @ I, 1)
End
Else
Begin
Set @ t2 = @ t2 + substring (@ str, @ I, 1)
End
Set @ I = @ I + 1

End
 
Set @ tmp = (10-(@ t1 * 3 + @ t2) % 10) % 10
 
-- End function
SELECT @ t1, @ t2, @ tmp
Declare @ str as varchar (15)
Declare @ tmp as varchar (30)
Declare @ t1 as int
Declare @ t2 as int
Declare @ I int
Set @ t1 = 0
Set @ t2 = 0
Set @ str = '20140901'
Set @ I = 1
While @ I <= 12
Begin
If @ I % 2 = 0
Begin
Set @ t1 = @ t1 + substring (@ str, @ I, 1)
End
Else
Begin
Set @ t2 = @ t2 + substring (@ str, @ I, 1)
End
Set @ I = @ I + 1
 
End

Set @ tmp = (10-(@ t1 * 3 + @ t2) % 10) % 10

-- End function
SELECT @ t1, @ t2, @ tmp www.2cto.com
ENA-13 verification code calculation method code position serial number refers to including the checkcode, from right to left sequential number (checkcode code position serial number is 1 ). The verification code is calculated as follows: a. the sum of all the even numbers starting from the code position 2. B. multiply the sum of Step a by 3. C. sum the numeric codes of all odd digits starting from code position 3. D. add the result of Step B and step c. E. subtract the result from step d with a number greater than or equal to step d and a minimum integer multiple of 10. The difference is the value of the obtained verification code. Example: the code 690123456789X1 is calculated in Table 1. Table 1 steps for calculating the verification code 1. from right to left sequence number position No. 13 12 11 10 9 8 8 6 5 5 4 3 2 1 Code 6 9 0 1 2 3 4 5 6 7 8 9X2. start from number 2 and find the sum of numbers in the even bits ① 9 + 7 + 5 + 3 + 1 + 9 = 34 ① 3. ① * 3 = ② 34 × 3 = 102 ② 4. calculate the sum of numbers in the odd digit (3) 8 + 6 + 4 + 2 + 0 + 6 = 26 (3) 5. ② + ③ = ④ 102 + 26 = 128 ④ 6. subtract (4) from the number greater than or equal to result 4, which is 10 smallest integer multiple. The difference is the value of the checkcode 130-128 = 2 checkcode X1 = 2.

From Tao2581 notes

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.