Barcode Check digit calculation formula PHP, T-SQL program _php Tutorial

Source: Internet
Author: User
Recent work involves the calculation of barcode 13th bit Check bit batch change, AI, barcode printer, barcode scanner and other software built-in check bit, online search also found bar code generator, but for us is not flexible enough to write a small program calculation check bit

OK First look at the PHP version, and online search to a lot simpler


function En13 ($code)
{
$tmp 1=0;
$tmp 2=0;
for ($i =0; $i <12; $i + +)
{
if ($i% 2==0)
$tmp 1+=substr ($code, $i, 1);
Else
$tmp 2+=substr ($code, $i, 1);
}
Return (10-($tmp 2*3+ $tmp 1)%10)%10;
}

Test www.2cto.com
$t = "6939762911740";
echo En13 ($t);
function En13 ($code)
{
$tmp 1=0;
$tmp 2=0;
for ($i =0; $i <12; $i + +)
{
if ($i% 2==0)
$tmp 1+=substr ($code, $i, 1);
Else
$tmp 2+=substr ($code, $i, 1);
}
Return (10-($tmp 2*3+ $tmp 1)%10)%10;
}

Test
$t = "6939762911740";
echo En13 ($t);
Also have to the MSSQL database data in batch update do not write programs directly in SQL Enterprise Manager update

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 = ' 6939762911740 '
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 = ' 6939762911740 '
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
The calculation method of the ENA-13 check code position ordinal code position sequence number refers to including the checksum, the right-to-left sequence numbers (the code position of the check code is ordinal 1). The calculation steps of the verification code are as follows: A. Starting with the code position ordinal 2, the numeric code for all even digits is summed. b. Multiply the sum of steps a by 3. C. Starting with the code position ordinal 3, the numeric code for all odd digits is summed. D. Add step B to the result of step c. E. With the result of greater than or equal to step d and the number of 10 min integer times minus step D results, the difference is the value of the check code to be obtained. Example: The calculation of code 690123456789x1 check code is shown in table 1. Table 1 The method of calculating the verification code example 1. Right-to-left sequential numbering position ordinal 13 12 11 10 9 8 7 6 5 4 3 2 1 Code 6 9 0 1 2 3 4 5 6 7 8 9 X 2. The sum of the numbers on the even digits is calculated from the ordinal 2 ①9+7+ 5+3+1+9=34①3. ①*3=②34x3=102②4. Starting with the ordinal 3, the sum of the numbers on the odd digits is calculated and ③8+6+4+2+0+6=26③5. ②+③=④102+26=128④6. Subtract ④ with a number greater than or equal to the result ④ and 10 minimum integer times, the difference is the value of the check code to be obtained 130-128=2 check code x1=2

Excerpt from Tao2581 Daily Diary

http://www.bkjia.com/PHPjc/478484.html www.bkjia.com true http://www.bkjia.com/PHPjc/478484.html techarticle Recent work involves the calculation of barcode 13th bit Check bit batch change, AI, barcode printer, barcode scanner and other software built-in check bit, online search also found bar code generator, but to ...

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