Wednesday, May 3, 2017

Convert a UPC-A to a GTIN-14

More and more I'm having to use GTIN barcodes instead of the old UPC-A bar codes. I needed a way to automatically grab the UPC-A bar code number from our database, strip the check digit, and generate a new GTIN-14 bar code on the fly, with a newly recalculated check digit. This is done in PHP, so take a whack at it and have fun! I'm still investigating how to produce bearer bars.

The trick to generating a check digit is list the 12 digit UPC-A with two zeros as a prefix, and starting with the first position on the left, mark the numbers as the Odd and Even positions, leaving off the last digit (check digit).

00  6631    2102    3756
OE OEOE OEOE OEOX

Add all the Odd positions together :19
Add all the Even positions together: 17

Multiply the Odd total by 3: 19*3=57

add the Odd and Even totals together: 57+17=74

If a multiple of 10, check digit is 0. In our example case it's not, so if not a multiple of 10, Subtract from the next higher multiple of 10: 80-74 = 6

6 is the check digit. Now add a 01 prefix in front and you have a GTIN-14 compatible bar code.

01 0066312102375 6


Code:
https://pastebin.com/32QRkPZ5
Related Posts Plugin for WordPress, Blogger...