<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; upc</title>
	<atom:link href="http://www.codediesel.com/tag/upc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Generating UPC check digit</title>
		<link>http://www.codediesel.com/php/generating-upc-check-digit/</link>
		<comments>http://www.codediesel.com/php/generating-upc-check-digit/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 06:10:58 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[upc]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1610</guid>
		<description><![CDATA[auto generation of upc check digit]]></description>
			<content:encoded><![CDATA[<p>The following small code generates check digit for UPC-A codes. You can use it for example to generate random UPC-A codes.</p>
<h4>Check digit calculation</h4>
<p>The check digit is calculated as follows:<br />
1. Add the digits in the odd-numbered positions together and multiply by three.<br />
2. Add the digits in the even-numbered positions to the above result.<br />
3. Find the result modulo 10 (i.e. the remainder when the result is divided by 10).<br />
4. If the result is not zero, subtract the result from ten.<br />
<span id="more-1610"></span></p>
<h4>Code</h4>

<div class="wp_codebox"><table><tr id="p16101"><td class="code" id="p1610code1"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> generate_upc_checkdigit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$upc_code</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$odd_total</span>  <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$even_total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #009933; font-style: italic;">/* Sum even digits */</span>
            <span style="color: #000088;">$even_total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$upc_code</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #009933; font-style: italic;">/* Sum odd digits */</span>
            <span style="color: #000088;">$odd_total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$upc_code</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$odd_total</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$even_total</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Get the remainder MOD 10*/</span>
    <span style="color: #000088;">$check_digit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sum</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* If the result is not zero, subtract the result from ten. */</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$check_digit</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">10</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$check_digit</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$check_digit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> generate_upc_checkdigit<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;72438451112&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/generating-upc-check-digit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

