<?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>freemind blogs &#187; c#</title>
	<atom:link href="http://blogs.freemind.net/index.php/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.freemind.net</link>
	<description></description>
	<lastBuildDate>Thu, 19 Nov 2009 04:49:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using WCF REST Starter Kit to Access Constant Contact API</title>
		<link>http://blogs.freemind.net/index.php/using-wcf-rest-starter-kit-to-access-constant-contact-api/57/</link>
		<comments>http://blogs.freemind.net/index.php/using-wcf-rest-starter-kit-to-access-constant-contact-api/57/#comments</comments>
		<pubDate>Fri, 01 May 2009 19:30:37 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[atompub]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[wcf]]></category>

		<guid isPermaLink="false">http://blogs.freemind.net/?p=57</guid>
		<description><![CDATA[Problem: Allow customers to sign up for email newsletters and synchronize those accounts with Constant Contact. Constant Contact is a company that provides Email Marketing services. You can use their editor to create nice looking emails and then configure a list of contacts. Constant Contact handles all the email sending, bounces, and unsubscribe activities. They [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: Allow customers to sign up for email newsletters and synchronize those accounts with Constant Contact.</p>
<p><a title="Constant Contact" href="http://constantcontact.com" target="_blank">Constant Contact</a> is a company that provides Email Marketing services. You can use their editor to create nice looking emails and then configure a list of contacts. Constant Contact handles all the email sending, bounces, and unsubscribe activities. They also provide excellent reporting features to track how many people actually view your emails or click on the contained links.</p>
<p>The nice folks at Constant Contact provide a set of <a href="http://developer.constantcontact.com/doc" target="_blank">web services </a>for accessing account data. These interfaces are implemented as REST services utilizing the Atom Publication Protocol (AtomPub) and Atom Syndication Format. At first it might seem odd to be accessing data objects via a <em>blogging</em> protocol but it actually works quite naturally. So well that Google&#8217;s GData and Microsoft&#8217;s ADO.NET Data Services also support this technique.</p>
<p>Writing all the HTTP, Auth, and XML parsing code can be a real drag. Luckily Microsoft is releasing a <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24644" target="_blank">WCF REST Starter Kit</a>. From the CodePlex summary:</p>
<blockquote><p><span id="ctl00_ctl00_MasterContent_Content_wikiSourceLabel">The WCF REST Starter Kit is a set of .Net Framework classes and Visual Studio features and templates that enable users to create and access REST-style Windows Communication Foundation (WCF) services. These services are based on the WCF web programming model available in .Net 3.5 SP1. The starter kit also contains the full source code for all features, detailed code samples, and unit tests.</span></p></blockquote>
<p>This starter kit makes accessing the Constant Contact API quite simple. Call GetEntry or GetFeed with a URI and get back a SyndicationItem or SyndicationFeed (collection of items). Similar methods exist for adding and updating data (AddEntry and UpdateEntry).</p>
<p>The following is one method from my ContactListProvider class:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> ContactList GetContactList<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> id<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;AtomPubClient client <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AtomPubClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;client<span style="color: #008000;">.</span><span style="color: #0000FF;">TransportSettings</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> GetLoginCredentials<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Place in a try block to ensure that any errors are caught</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; SyndicationItem item <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEntry</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; ContactList list <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ContactList<span style="color: #008000;">&#40;</span>item<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #0600FF; font-weight: bold;">as</span> XmlSyndicationContent<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> list<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>WebException ex<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; _log<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WebException: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Status</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span>, ex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Get the exception type</span><br />
&nbsp; &nbsp; &nbsp; _log<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Exception: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span>, ex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Contact me via comments if you want a copy of all the providers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.freemind.net/index.php/using-wcf-rest-starter-kit-to-access-constant-contact-api/57/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to convert UPC-A to zero-compressed UPC-E</title>
		<link>http://blogs.freemind.net/index.php/how-to-convert-upc-a-to-zero-compressed-upc-e/56/</link>
		<comments>http://blogs.freemind.net/index.php/how-to-convert-upc-a-to-zero-compressed-upc-e/56/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 22:59:32 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[upc]]></category>
		<category><![CDATA[yucky]]></category>

		<guid isPermaLink="false">http://blogs.freemind.net/?p=56</guid>
		<description><![CDATA[Universal Product Codes (UPCs) should be a very straight-forward topic. You give a product a number and that is the end of the story. Unfortunately it turns out to be a bit more complicated than that. There is a great (well I guess that&#8217;s subjective) page on Wikipedia that does an excellent job explaining all [...]]]></description>
			<content:encoded><![CDATA[<p>Universal Product Codes (UPCs) should be a very straight-forward topic. You give a product a number and that is the end of the story. Unfortunately it turns out to be a bit more complicated than that.</p>
<p>There is a great (well I guess that&#8217;s subjective) page on Wikipedia that does an excellent job explaining all the variations: <a title="Universal Product Codes" href="http://en.wikipedia.org/wiki/Universal_Product_Code" target="_blank">Universal Product Codes</a>. There are 14, 13, 12, 11, 10, 8, and 6 digit types of UPCs. There is also an interesting algorithm to convert from a fairly standard 12-digit UPC-A code into a zero-compressed UPC-E. This is a most uninteresting fact. Until the day comes when you actually have to convert some of these codes.</p>
<p>Here is some C# code to:</p>
<ul>
<li>Convert UPC-A codes to UPC-E</li>
<li>Convert UPC-E codes to UPC-A</li>
<li>Calculate check digits</li>
</ul>
<p>If google brought you here because you need these conversions then bask is it&#8217;s yucky goodness! If not, then just move right along (basically it&#8217;s a cup with dirt in it).</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> ConvertUPCAToUPCE<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UPCa<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #6666cc; font-weight: bold;">string</span> UPCe <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//Must be 12 digits</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>UPCa<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">12</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #6666cc; font-weight: bold;">string</span> mfg <span style="color: #008000;">=</span> UPCa<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #6666cc; font-weight: bold;">string</span> prod <span style="color: #008000;">=</span> UPCa<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">6</span>, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;000&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span>mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;100&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span><span style="color: #008000;">&#40;</span>mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;200&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXNNN0 &nbsp;0XX000-00NNN + check</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXNNN1 &nbsp;0XX100-00NNN + check</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXNNN2 &nbsp;0XX200-00NNN + check</span><br />
&nbsp; &nbsp; &nbsp; UPCe <span style="color: #008000;">=</span> mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> prod<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>,<span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;00&quot;</span><span style="color: #008000;">&#41;</span> &nbsp; <br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXXNN3 &nbsp;0XXX00-000NN + check</span><br />
&nbsp; &nbsp; &nbsp; UPCe <span style="color: #008000;">=</span> mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>,<span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> prod<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>,<span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;3&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;4 &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XXXXN4 &nbsp;0XXXX0-0000N + check</span><br />
&nbsp; &nbsp; &nbsp; UPCe <span style="color: #008000;">=</span> mfg<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> prod<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span>,<span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;4&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXXXX5 &nbsp;0XXXXX-00005 + check</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXXXX6 &nbsp;0XXXXX-00006 + check</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;7 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXXXX7 &nbsp;0XXXXX-00007 + check</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXXXX8 &nbsp;0XXXXX-00008 + check</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XXXXX9 &nbsp;0XXXXX-00009 + check</span><br />
&nbsp; &nbsp; &nbsp; UPCe <span style="color: #008000;">=</span> mfg <span style="color: #008000;">+</span> prod<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">return</span> UPCe<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> ConvertUPCEToUPCA<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> UPCe<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//Must be 12 digits</span><br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">6</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #6666cc; font-weight: bold;">string</span> mfg <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #6666cc; font-weight: bold;">string</span> prod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mfg <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;000&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;00&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;1&quot;</span><span style="color: #008000;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mfg <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;100&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;00&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;2&quot;</span><span style="color: #008000;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mfg <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;200&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;00&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;3&quot;</span><span style="color: #008000;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mfg <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;00&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;000&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;4&quot;</span><span style="color: #008000;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mfg <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0000&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mfg <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prod <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0000&quot;</span> <span style="color: #008000;">+</span> UPCe<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">return</span> mfg <span style="color: #008000;">+</span> prod <span style="color: #008000;">+</span> CalculateCheckDigit<span style="color: #008000;">&#40;</span>mfg <span style="color: #008000;">+</span> prod<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//In the UPC-A system, the check digit is calculated as follows:</span><br />
<span style="color: #008080; font-style: italic;">//</span><br />
<span style="color: #008080; font-style: italic;">// &nbsp;1. Add the digits in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.</span><br />
<span style="color: #008080; font-style: italic;">// &nbsp;2. Add the digits in the even-numbered positions (second, fourth, sixth, etc.) to the result.</span><br />
<span style="color: #008080; font-style: italic;">// &nbsp;3. Find the result modulo 10 (i.e. the remainder when the result is divided by 10).</span><br />
<span style="color: #008080; font-style: italic;">// &nbsp;4. If the result is not zero, subtract the result from ten.</span><br />
<span style="color: #008080; font-style: italic;">//</span><br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> CalculateCheckDigit<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> upc<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #6666cc; font-weight: bold;">int</span> check <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> chars <span style="color: #008000;">=</span> upc<span style="color: #008000;">.</span><span style="color: #0000FF;">ToCharArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// process string from right to left</span><br />
&nbsp; &nbsp;Array<span style="color: #008000;">.</span><span style="color: #0000FF;">Reverse</span><span style="color: #008000;">&#40;</span>chars<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>i<span style="color: #008000;">&lt;</span>chars<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>i <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// even</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;check <span style="color: #008000;">+=</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>chars<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// odd</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;check <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span> <span style="color: #008000;">*</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>chars<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;check <span style="color: #008000;">=</span> check <span style="color: #008000;">%</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>check <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; check <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span> <span style="color: #008000;">-</span> check<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF; font-weight: bold;">return</span> check<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Update: CheckDigit right to left processing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.freemind.net/index.php/how-to-convert-upc-a-to-zero-compressed-upc-e/56/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
