<?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>john.geek.nz - 38911 Basic Bytes Free &#187; C# Tips</title>
	<atom:link href="http://www.john.geek.nz/category/tips/csharp-tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.john.geek.nz</link>
	<description>SQL Tips, Apple Tips and Randomness</description>
	<lastBuildDate>Tue, 17 Jan 2012 20:18:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C# equivalent to VB val() function</title>
		<link>http://www.john.geek.nz/2009/08/c-equivalent-to-vb-val-function/</link>
		<comments>http://www.john.geek.nz/2009/08/c-equivalent-to-vb-val-function/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 19:35:12 +0000</pubDate>
		<dc:creator>John Burns</dc:creator>
				<category><![CDATA[C# Tips]]></category>

		<guid isPermaLink="false">http://www.john.geek.nz/?p=1345</guid>
		<description><![CDATA[Although VB had it&#8217;s limitations, it was damn easy to filter an input string for a numerical value. Here is how to do it in C#. You&#8217;ll need to use System.Text.RegularExpressions in the header private static int VBVal&#40;string sInput&#41; &#123; string sOutput = string.Empty; MatchCollection oMatches = Regex.Matches&#40;sInput, &#34;\\d+&#34;&#41;; foreach &#40;Match oMatch in oMatches&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Although VB had it&#8217;s limitations, it was damn easy to filter an input string for a numerical value.</p>
<p>Here is how to do it in C#.  You&#8217;ll need to use System.Text.RegularExpressions in the header</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> VBVal<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> sInput<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> sOutput <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
        MatchCollection oMatches <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Matches</span><span style="color: #008000;">&#40;</span>sInput, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>d+&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Match oMatch <span style="color: #0600FF; font-weight: bold;">in</span> oMatches<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
                sOutput <span style="color: #008000;">+=</span> oMatch<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>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>sOutput<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

 <p><a href="http://www.john.geek.nz/?flattrss_redirect&amp;id=1345&amp;md5=d418019bb2728ccf38ba67992da3ee46" title="Flattr" target="_blank"><img src="http://www.john.geek.nz/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.john.geek.nz/2009/08/c-equivalent-to-vb-val-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=johnburns007&amp;popout=1&amp;url=http%3A%2F%2Fwww.john.geek.nz%2F2009%2F08%2Fc-equivalent-to-vb-val-function%2F&amp;language=en_GB&amp;category=text&amp;title=C%23+equivalent+to+VB+val%28%29+function&amp;description=Although+VB+had+it%26%238217%3Bs+limitations%2C+it+was+damn+easy+to+filter+an+input+string+for+a+numerical+value.+Here+is+how+to+do+it+in+C%23.+You%26%238217%3Bll+need+to+use...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Image Editing in C# &#8211; Contrast</title>
		<link>http://www.john.geek.nz/2009/03/image-editing-in-c-contrast/</link>
		<comments>http://www.john.geek.nz/2009/03/image-editing-in-c-contrast/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 16:25:35 +0000</pubDate>
		<dc:creator>John Burns</dc:creator>
				<category><![CDATA[C# Tips]]></category>

		<guid isPermaLink="false">http://www.john.geek.nz/?p=1192</guid>
		<description><![CDATA[When attempting to get machines to process images you sometimes run into noise problems. Filters are regularly applied first to clean them up. Editing the contrast of an image changes its dynamic range. In its most basic form, it will move pixel values away from the center (normally 128 if using 8 bits per color). [...]]]></description>
			<content:encoded><![CDATA[<p>When attempting to get machines to process images you sometimes run into noise problems.  Filters are regularly applied first to clean them up.</p>
<p>Editing the contrast of an image changes its dynamic range.  In its most basic form, it will move pixel values away from the center (normally 128 if using 8 bits per color).</p>
<p>There are many different ways to to perform contrast adjustment.<br />
<span id="more-1192"></span><br />
The basic process is:</p>
<ul>
<li>For each pixel in the image&#8230;</li>
<li>Divide each of the red, green and blue values by 255 to get a decimal value between 0 and 1
<li>
<li>Subtract 0.5 from each of these values to get a delta from average</li>
<li>Multiply each of these value by a contrast ratio</li>
<li>Add 0.5 back to each value</li>
<li>Multiply each value by 255</li>
<li>Change any values less than 0 to 0</li>
<li>Change any values greater than 255 to 255</li>
</ul>
<p>The contrast ratio is sometimes calculated from a RMS of the entire image contrast.<br />
In this simple (but effective) case:</p>
<ul>
<li>Take a contrast adjustment value between -100 and 100</li>
<li>Add 100 to the value and divide by 100 (To get a value between 0 and 2)</li>
<li>Square this result (Now between 0 and 4)</li>
</ul>
<p>An input contrast adjustment value of 10 will result in a contrast ratio of (110/100)^2 = 1.21 and the following results:<br />
0,0,0 &#8211; > 0,0,0<br />
64,64,64 -> 51,51,51<br />
128,128,128 &#8211; > 128,128,128<br />
192,192,192 -> 208,208,208<br />
255,255,255 -> 255,255,255</p>
<p>As you can see, the darkest, brightest and middle shades are retained, everything else is exponentially pushed towards one of the two extremes.</p>
<p>Here is some C# code to perform the steps above.  It takes a Bitmap type and a byte value between -100 and 100.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Bitmap Contrast<span style="color: #008000;">&#40;</span>Bitmap oBitmap, <span style="color: #6666cc; font-weight: bold;">sbyte</span> iContrast<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>iContrast <span style="color: #008000;">&lt;</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> oBitmap<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>iContrast <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> oBitmap<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">double</span> pixel <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//convert contrast to a decimal value</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> contrast <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">100.0</span> <span style="color: #008000;">+</span> iContrast<span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">100.0</span><span style="color: #008000;">;</span>
&nbsp;
    contrast <span style="color: #008000;">*=</span> contrast<span style="color: #008000;">;</span>
&nbsp;
    BitmapData bmData <span style="color: #008000;">=</span> oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">LockBits</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>, oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">&#41;</span>, ImageLockMode<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadWrite</span>, PixelFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">Format24bppRgb</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">int</span> stride <span style="color: #008000;">=</span> bmData<span style="color: #008000;">.</span><span style="color: #0000FF;">Stride</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IntPtr</span> Scan0 <span style="color: #008000;">=</span> bmData<span style="color: #008000;">.</span><span style="color: #0000FF;">Scan0</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">unsafe</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">*</span> p <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">void</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>Scan0<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">int</span> iRowEnd <span style="color: #008000;">=</span> stride <span style="color: #008000;">-</span> oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
&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> y <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> y <span style="color: #008000;">&lt;</span> oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>y<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;</span> oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>x<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> color <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> color <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> color<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008080; font-style: italic;">//0 = Blue, 1 = Green, 2 = Red</span>
                <span style="color: #008000;">&#123;</span>
&nbsp;
                    pixel <span style="color: #008000;">=</span> p<span style="color: #008000;">&#91;</span>color<span style="color: #008000;">&#93;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">255.0</span><span style="color: #008000;">;</span>
                    pixel <span style="color: #008000;">-=</span> <span style="color: #FF0000;">0.5</span><span style="color: #008000;">;</span>
                    pixel <span style="color: #008000;">*=</span> contrast<span style="color: #008000;">;</span>
                    pixel <span style="color: #008000;">+=</span> <span style="color: #FF0000;">0.5</span><span style="color: #008000;">;</span>
                    pixel <span style="color: #008000;">*=</span> <span style="color: #FF0000;">255</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pixel <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> pixel <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pixel <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">255</span><span style="color: #008000;">&#41;</span> pixel <span style="color: #008000;">=</span> <span style="color: #FF0000;">255</span><span style="color: #008000;">;</span>
                    p<span style="color: #008000;">&#91;</span>color<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span>pixel<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                p <span style="color: #008000;">+=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            p <span style="color: #008000;">+=</span> iRowEnd<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">UnlockBits</span><span style="color: #008000;">&#40;</span>bmData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> oBitmap<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

 <p><a href="http://www.john.geek.nz/?flattrss_redirect&amp;id=1192&amp;md5=5bcb0218409d7aa1c9ea16900d5dfb78" title="Flattr" target="_blank"><img src="http://www.john.geek.nz/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.john.geek.nz/2009/03/image-editing-in-c-contrast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=johnburns007&amp;popout=1&amp;url=http%3A%2F%2Fwww.john.geek.nz%2F2009%2F03%2Fimage-editing-in-c-contrast%2F&amp;language=en_GB&amp;category=text&amp;title=Image+Editing+in+C%23+%26%238211%3B+Contrast&amp;description=When+attempting+to+get+machines+to+process+images+you+sometimes+run+into+noise+problems.+Filters+are+regularly+applied+first+to+clean+them+up.+Editing+the+contrast+of+an+image+changes...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Image Editing in C# &#8211; Greyscale</title>
		<link>http://www.john.geek.nz/2009/03/image-editing-in-c-gryscale/</link>
		<comments>http://www.john.geek.nz/2009/03/image-editing-in-c-gryscale/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 15:33:04 +0000</pubDate>
		<dc:creator>John Burns</dc:creator>
				<category><![CDATA[C# Tips]]></category>

		<guid isPermaLink="false">http://www.john.geek.nz/?p=1187</guid>
		<description><![CDATA[I&#8217;m in the process of writing some code to detect the stars in astromical images. One of the filters I need to apply to an image is to convert it to greyscale. Conversion to greyscale is relatively straightforward &#8211; The red, green and blue values of a pixel all need to be set to the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m in the process of writing some code to detect the stars in astromical images.  One of the filters I need to apply to an image is to convert it to greyscale.</p>
<p>Conversion to greyscale is relatively straightforward &#8211; The red, green and blue values of a pixel all need to be set to the same value.</p>
<p>There is one caveat, and that is that our eyes are sensitive to different colors. This means that color images tend to have lots of blue and red when compared to green.<br />
<span id="more-1187"></span><br />
A typical formula to use is:<br />
whitevalue = (red * .30) + (green * .59) + (blue * .11)</p>
<p>Note that in the above formula, the three decimal values add to 1.00.  This means that white in the image is preserved and the range of 0-255 is not exceeded.</p>
<p>Here is some C# code to convert an image to greyscale.  It takes a Bitmap type and returns a Bitmap type.   It uses unsafe code for speed so you will need to compile with the allow unsafe code option.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> imageFilters
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> Bitmap greyScale<span style="color: #008000;">&#40;</span>Bitmap oBitmap<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        BitmapData bmData <span style="color: #008000;">=</span> oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">LockBits</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span>,
            oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">&#41;</span>, ImageLockMode<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadWrite</span>, PixelFormat<span style="color: #008000;">.</span><span style="color: #0000FF;">Format24bppRgb</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">int</span> stride <span style="color: #008000;">=</span> bmData<span style="color: #008000;">.</span><span style="color: #0000FF;">Stride</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IntPtr</span> Scan0 <span style="color: #008000;">=</span> bmData<span style="color: #008000;">.</span><span style="color: #0000FF;">Scan0</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">unsafe</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">*</span> ptr <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">void</span><span style="color: #008000;">*</span><span style="color: #008000;">&#41;</span>Scan0<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">int</span> iRowEnd <span style="color: #008000;">=</span> stride <span style="color: #008000;">-</span> <span style="color: #008000;">&#40;</span>oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&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> y <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> y <span style="color: #008000;">&lt;</span> oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>y<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;</span> oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>x<span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">//GDI returns BGR rather than RGB</span>
                    <span style="color: #008080; font-style: italic;">//Hence ptr[0] is Blue NOT Red</span>
                    ptr<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> ptr<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> ptr<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span>
                        <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">.</span>30 <span style="color: #008000;">*</span> ptr<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">.</span>59 <span style="color: #008000;">*</span> ptr<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">.</span>11 <span style="color: #008000;">*</span> ptr<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    ptr <span style="color: #008000;">+=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                ptr <span style="color: #008000;">+=</span> iRowEnd<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        oBitmap<span style="color: #008000;">.</span><span style="color: #0000FF;">UnlockBits</span><span style="color: #008000;">&#40;</span>bmData<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> oBitmap<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Enjoy&#8230;.</p>
 <p><a href="http://www.john.geek.nz/?flattrss_redirect&amp;id=1187&amp;md5=b561c15dbee37185db8ff7cd1aa79579" title="Flattr" target="_blank"><img src="http://www.john.geek.nz/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.john.geek.nz/2009/03/image-editing-in-c-gryscale/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=johnburns007&amp;popout=1&amp;url=http%3A%2F%2Fwww.john.geek.nz%2F2009%2F03%2Fimage-editing-in-c-gryscale%2F&amp;language=en_GB&amp;category=text&amp;title=Image+Editing+in+C%23+%26%238211%3B+Greyscale&amp;description=I%26%238217%3Bm+in+the+process+of+writing+some+code+to+detect+the+stars+in+astromical+images.+One+of+the+filters+I+need+to+apply+to+an+image+is+to+convert+it...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>TLEs &#8211; Calculation of the Julian Day Number (JDN)</title>
		<link>http://www.john.geek.nz/2009/03/tles-calculation-of-the-julian-day-number-jdn/</link>
		<comments>http://www.john.geek.nz/2009/03/tles-calculation-of-the-julian-day-number-jdn/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 02:28:44 +0000</pubDate>
		<dc:creator>John Burns</dc:creator>
				<category><![CDATA[Astronomy]]></category>
		<category><![CDATA[C# Tips]]></category>

		<guid isPermaLink="false">http://www.john.geek.nz/?p=1140</guid>
		<description><![CDATA[This post is part of a series on understanding the meaning behind the values contained in a Two Line Element set, or Keplerian Elements. The main article is here. Calculation of the Julian Day Number (JDN) Julian day numbers are regularly used in astronomy instead of a date. Julian day numbers are simply the count [...]]]></description>
			<content:encoded><![CDATA[<p>This post is part of a series on understanding the meaning behind the values contained in a Two Line Element set, or Keplerian Elements.  The main article is <a href="http://www.john.geek.nz/index.php/2009/03/understanding-satellite-two-line-element-sets/">here</a>.</p>
<h3>Calculation of the Julian Day Number (JDN)</h3>
<p>Julian day numbers are regularly used in astronomy instead of a date.</p>
<p>Julian day numbers are simply the count of days that have elapse since noon on January 1, 4713 BC Greenwich time using the  Julian proleptic calendar.<br />
<span id="more-1140"></span><br />
We generally use the Gregorian calendar.  Calculation to a Julian date is a little cryptic as the two calendars have different rules for calculating leap years.</p>
<ul>
Julian Calendar</p>
<li>Every 4 years</li>
</ul>
<ul>
Gregorian Calendar</p>
<li>Every 4 years except when the year is divisble by 100</li>
<li>But ALWAYS every 400 years</li>
</ul>
<p>To calculate the value, we first get the Julian date of the year, then add on the days in the year followed by the fraction of the day.</p>
<p>Some astronomers like to use a Modified Julian Date.  This is simply the Julian Date with 2,400,000.5 subtracted from it making the number smaller and based on midnight rather than noon UTC.</p>
<p>Here is some C# which will calculate the following:</p>
<ul>
<li>Julian Day from a DateTime</li>
<li>Julian Day for a year</li>
<li>Julian Day from a Satellite Epoch</li>
<li>Converting between modified and standard Julian dates</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">double</span> getJulianDay_DateTime<span style="color: #008000;">&#40;</span>DateTime date<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//Calculate the day of the year and the</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> dDay <span style="color: #008000;">=</span> date<span style="color: #008000;">.</span><span style="color: #0000FF;">DayOfYear</span><span style="color: #008000;">;</span>
    dDay <span style="color: #008000;">+=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDouble</span><span style="color: #008000;">&#40;</span>date<span style="color: #008000;">.</span><span style="color: #0000FF;">Hour</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">24</span><span style="color: #008000;">;</span>
    dDay <span style="color: #008000;">+=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDouble</span><span style="color: #008000;">&#40;</span>date<span style="color: #008000;">.</span><span style="color: #0000FF;">Minute</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">1440</span><span style="color: #008000;">;</span>
    dDay <span style="color: #008000;">+=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDouble</span><span style="color: #008000;">&#40;</span>date<span style="color: #008000;">.</span><span style="color: #0000FF;">Second</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">86400</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> dDay <span style="color: #008000;">+</span> getJulianDay_Year<span style="color: #008000;">&#40;</span>date<span style="color: #008000;">.</span><span style="color: #0000FF;">Year</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">double</span> getJulianDay_Year<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> year<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> dYear <span style="color: #008000;">=</span> year <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> A <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Floor</span><span style="color: #008000;">&#40;</span>dYear <span style="color: #008000;">/</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> B <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">-</span> A <span style="color: #008000;">+</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Floor</span><span style="color: #008000;">&#40;</span>A <span style="color: #008000;">/</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//The use of 30.600000000000001 is to correct for floating point rounding problems</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> dResult <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Floor</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">365.25</span> <span style="color: #008000;">*</span> dYear<span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1721422.9</span> <span style="color: #008000;">+</span> B<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> dResult<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">double</span> getJulianDay_SatEpoch<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> year, <span style="color: #6666cc; font-weight: bold;">double</span> dSatelliteEpoch<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//Tidy up the year and put it into the correct century</span>
    year <span style="color: #008000;">=</span> year <span style="color: #008000;">%</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>year <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">57</span><span style="color: #008000;">&#41;</span> year <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2000</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> year <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1900</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">double</span> dResult <span style="color: #008000;">=</span> getJulianDay_Year<span style="color: #008000;">&#40;</span>year<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    dResult <span style="color: #008000;">+=</span> dSatelliteEpoch<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> dResult<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">double</span> getModifiedJulian_Julian<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> dJulian<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> dJulian <span style="color: #008000;">-</span> <span style="color: #FF0000;">2400000.5</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">double</span> getJulian_ModifiedJulian<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> dModifiedJulian<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> dModifiedJulian <span style="color: #008000;">+</span> <span style="color: #FF0000;">2400000.5</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

 <p><a href="http://www.john.geek.nz/?flattrss_redirect&amp;id=1140&amp;md5=5c176d0107cb66ea84deb908c0ce45f2" title="Flattr" target="_blank"><img src="http://www.john.geek.nz/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.john.geek.nz/2009/03/tles-calculation-of-the-julian-day-number-jdn/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=johnburns007&amp;popout=1&amp;url=http%3A%2F%2Fwww.john.geek.nz%2F2009%2F03%2Ftles-calculation-of-the-julian-day-number-jdn%2F&amp;language=en_GB&amp;category=text&amp;title=TLEs+%26%238211%3B+Calculation+of+the+Julian+Day+Number+%28JDN%29&amp;description=This+post+is+part+of+a+series+on+understanding+the+meaning+behind+the+values+contained+in+a+Two+Line+Element+set%2C+or+Keplerian+Elements.+The+main+article+is+here.+Calculation...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Code: Shortest distance between any two line segments</title>
		<link>http://www.john.geek.nz/2009/03/code-shortest-distance-between-any-two-line-segments/</link>
		<comments>http://www.john.geek.nz/2009/03/code-shortest-distance-between-any-two-line-segments/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 10:47:45 +0000</pubDate>
		<dc:creator>John Burns</dc:creator>
				<category><![CDATA[C# Tips]]></category>

		<guid isPermaLink="false">http://www.john.geek.nz/?p=1060</guid>
		<description><![CDATA[I recently needed to find the shortest distance between any two line segments in 3D space. I managed to find some C++ code online at http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm which I converted to C#. It&#8217;s fairly easy to use. If you only need it for 2D space, just set the Z values to 0. Please post a comment [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to find the shortest distance between any two line segments in 3D space.</p>
<p>I managed to find some C++ code online at <a href="http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm" target="_blank">http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm</a> which I converted to C#.</p>
<p>It&#8217;s fairly easy to use.  If you only need it for 2D space, just set the Z values to 0.</p>
<p>Please post a comment if you find this useful.<br />
<span id="more-1060"></span><br />
Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/*
* Converted to C# by John Burns from C++ sourced from:
* http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
*
* Original C++ Copyright Notice
* ===================
* Copyright 2001, softSurfer (www.softsurfer.com)
* This code may be freely used and modified for any purpose
* providing that this copyright notice is included with it.
* SoftSurfer makes no warranty for this code, and cannot be held
* liable for any real or imagined damage resulting from its use.
* Users of this code must verify correctness for their application.
*/</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> point
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> x<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> y<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> z<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> line
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> x1<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> y1<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> z1<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> x2<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> y2<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> z2<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">double</span> getShortestDistance<span style="color: #008000;">&#40;</span>line line1, line line2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> EPS <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.00000001</span><span style="color: #008000;">;</span>
&nbsp;
    point delta21 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> point<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    delta21<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">=</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">x2</span> <span style="color: #008000;">-</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">x1</span><span style="color: #008000;">;</span>
    delta21<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span> <span style="color: #008000;">=</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">y2</span> <span style="color: #008000;">-</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">y1</span><span style="color: #008000;">;</span>
    delta21<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">=</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">z2</span> <span style="color: #008000;">-</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">z1</span><span style="color: #008000;">;</span>
&nbsp;
    point delta41 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> point<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    delta41<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">=</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">x2</span> <span style="color: #008000;">-</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">x1</span><span style="color: #008000;">;</span>
    delta41<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span> <span style="color: #008000;">=</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">y2</span> <span style="color: #008000;">-</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">y1</span><span style="color: #008000;">;</span>
    delta41<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span> <span style="color: #008000;">=</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">z2</span> <span style="color: #008000;">-</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">z1</span><span style="color: #008000;">;</span>
&nbsp;
    point delta13 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> point<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    delta13<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">=</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">x1</span> <span style="color: #008000;">-</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">x1</span><span style="color: #008000;">;</span>
    delta13<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span> <span style="color: #008000;">=</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">y1</span> <span style="color: #008000;">-</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">y1</span><span style="color: #008000;">;</span>
    delta13<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span> <span style="color: #008000;">=</span> line1<span style="color: #008000;">.</span><span style="color: #0000FF;">z1</span> <span style="color: #008000;">-</span> line2<span style="color: #008000;">.</span><span style="color: #0000FF;">z1</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">double</span> a <span style="color: #008000;">=</span> dot<span style="color: #008000;">&#40;</span>delta21, delta21<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> b <span style="color: #008000;">=</span> dot<span style="color: #008000;">&#40;</span>delta21, delta41<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> c <span style="color: #008000;">=</span> dot<span style="color: #008000;">&#40;</span>delta41, delta41<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> d <span style="color: #008000;">=</span> dot<span style="color: #008000;">&#40;</span>delta21, delta13<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> e <span style="color: #008000;">=</span> dot<span style="color: #008000;">&#40;</span>delta41, delta13<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> D <span style="color: #008000;">=</span> a <span style="color: #008000;">*</span> c <span style="color: #008000;">-</span> b <span style="color: #008000;">*</span> b<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">double</span> sc, sN, sD <span style="color: #008000;">=</span> D<span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> tc, tN, tD <span style="color: #008000;">=</span> D<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>D <span style="color: #008000;">&lt;</span> EPS<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        sN <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">;</span>
        sD <span style="color: #008000;">=</span> <span style="color: #FF0000;">1.0</span><span style="color: #008000;">;</span>
        tN <span style="color: #008000;">=</span> e<span style="color: #008000;">;</span>
        tD <span style="color: #008000;">=</span> c<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        sN <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>b <span style="color: #008000;">*</span> e <span style="color: #008000;">-</span> c <span style="color: #008000;">*</span> d<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        tN <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>a <span style="color: #008000;">*</span> e <span style="color: #008000;">-</span> b <span style="color: #008000;">*</span> d<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sN <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            sN <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">;</span>
            tN <span style="color: #008000;">=</span> e<span style="color: #008000;">;</span>
            tD <span style="color: #008000;">=</span> c<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sN <span style="color: #008000;">&gt;</span> sD<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            sN <span style="color: #008000;">=</span> sD<span style="color: #008000;">;</span>
            tN <span style="color: #008000;">=</span> e <span style="color: #008000;">+</span> b<span style="color: #008000;">;</span>
            tD <span style="color: #008000;">=</span> c<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>tN <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        tN <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span>d <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">&#41;</span>
            sN <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span>d <span style="color: #008000;">&gt;</span> a<span style="color: #008000;">&#41;</span>
            sN <span style="color: #008000;">=</span> sD<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            sN <span style="color: #008000;">=</span> <span style="color: #008000;">-</span>d<span style="color: #008000;">;</span>
            sD <span style="color: #008000;">=</span> a<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>tN <span style="color: #008000;">&gt;</span> tD<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        tN <span style="color: #008000;">=</span> tD<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span>d <span style="color: #008000;">+</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">&#41;</span>
            sN <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span>d <span style="color: #008000;">+</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> a<span style="color: #008000;">&#41;</span>
            sN <span style="color: #008000;">=</span> sD<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            sN <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span>d <span style="color: #008000;">+</span> b<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            sD <span style="color: #008000;">=</span> a<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>sN<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> EPS<span style="color: #008000;">&#41;</span> sc <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> sc <span style="color: #008000;">=</span> sN <span style="color: #008000;">/</span> sD<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>tN<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> EPS<span style="color: #008000;">&#41;</span> tc <span style="color: #008000;">=</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span> tc <span style="color: #008000;">=</span> tN <span style="color: #008000;">/</span> tD<span style="color: #008000;">;</span>
&nbsp;
    point dP <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> point<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    dP<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">=</span> delta13<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span>sc <span style="color: #008000;">*</span> delta21<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">-</span> <span style="color: #008000;">&#40;</span>tc <span style="color: #008000;">*</span> delta41<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    dP<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span> <span style="color: #008000;">=</span> delta13<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span>sc <span style="color: #008000;">*</span> delta21<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">-</span> <span style="color: #008000;">&#40;</span>tc <span style="color: #008000;">*</span> delta41<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    dP<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span> <span style="color: #008000;">=</span> delta13<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span>sc <span style="color: #008000;">*</span> delta21<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">-</span> <span style="color: #008000;">&#40;</span>tc <span style="color: #008000;">*</span> delta41<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>dot<span style="color: #008000;">&#40;</span>dP, dP<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">double</span> dot<span style="color: #008000;">&#40;</span>point c1, point c2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>c1<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">*</span> c2<span style="color: #008000;">.</span><span style="color: #0000FF;">x</span> <span style="color: #008000;">+</span> c1<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span> <span style="color: #008000;">*</span> c2<span style="color: #008000;">.</span><span style="color: #0000FF;">y</span> <span style="color: #008000;">+</span> c1<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span> <span style="color: #008000;">*</span> c2<span style="color: #008000;">.</span><span style="color: #0000FF;">z</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">double</span> norm<span style="color: #008000;">&#40;</span>point c1<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>dot<span style="color: #008000;">&#40;</span>c1, c1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

 <p><a href="http://www.john.geek.nz/?flattrss_redirect&amp;id=1060&amp;md5=0241cc8a863d733b2009fd26a53caef4" title="Flattr" target="_blank"><img src="http://www.john.geek.nz/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.john.geek.nz/2009/03/code-shortest-distance-between-any-two-line-segments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=johnburns007&amp;popout=1&amp;url=http%3A%2F%2Fwww.john.geek.nz%2F2009%2F03%2Fcode-shortest-distance-between-any-two-line-segments%2F&amp;language=en_GB&amp;category=text&amp;title=Code%3A+Shortest+distance+between+any+two+line+segments&amp;description=I+recently+needed+to+find+the+shortest+distance+between+any+two+line+segments+in+3D+space.+I+managed+to+find+some+C%2B%2B+code+online+at+http%3A%2F%2Fsoftsurfer.com%2FArchive%2Falgorithm_0106%2Falgorithm_0106.htm+which+I+converted+to...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>SHA256 and higher in SQL Server</title>
		<link>http://www.john.geek.nz/2009/01/sha256-and-higher-in-sql-server/</link>
		<comments>http://www.john.geek.nz/2009/01/sha256-and-higher-in-sql-server/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 17:59:23 +0000</pubDate>
		<dc:creator>John Burns</dc:creator>
				<category><![CDATA[C# Tips]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.john.geek.nz/?p=973</guid>
		<description><![CDATA[As part of a new project I&#8217;m doing, I needed to use hashes. A good source of information on hashes is available from wikipedia. SQL has the built in function HASHBYTES but this only supports up to the SHA128 algorithm and I want to use SHA256. I created myself a CLR in C#.net and in [...]]]></description>
			<content:encoded><![CDATA[<p>As part of a new project I&#8217;m doing, I needed to use hashes.</p>
<p>A good source of information on hashes is available from <a href="http://en.wikipedia.org/wiki/SHA_hash_functions" tarbet="_blank">wikipedia</a>.</p>
<p>SQL has the built in function HASHBYTES but this only supports up to the SHA128 algorithm and I want to use SHA256.</p>
<p>I created myself a CLR in C#.net and in some moment of feeling generous, decided to share the code.</p>
<p><span id="more-973"></span></p>
<p>This code takes a string and returns the hash as a string however it is a simple process to return a binary instead.</p>
<p>Please comment if you find it useful.</p>
<p>To use it, you&#8217;ll need to:</p>
<ul>
<li>Compile the dll or <a href='http://www.john.geek.nz/wp-content/uploads/2009/01/cryptohashclr.zip'>download my precompiled one</a></li>
<li>Create the assembly in SQL</li>
<li>Create the user defined scalar function</li>
</ul>
<p>First of all, the (completely undocumented) csharp.  There is a link above to download it as a precompiled binary, otherwise fire up Visual Studio and compile the following in a database project.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data.SqlTypes</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Cryptography</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Server</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> cryptohashCLR
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> SqlHash
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000;">&#91;</span>SqlFunction<span style="color: #008000;">&#40;</span>DataAccess <span style="color: #008000;">=</span> DataAccessKind<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span>, IsDeterministic <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>, IsPrecise <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>, Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;udf_hashBytes&quot;</span>, SystemDataAccess <span style="color: #008000;">=</span> SystemDataAccessKind<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> SqlString CLRHash<span style="color: #008000;">&#40;</span>SqlString hashtype, SqlString input<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>input<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span> <span style="color: #008000;">||</span> hashtype<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> SqlString<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Null</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> plainTextBytes <span style="color: #008000;">=</span> Encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">UTF8</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>input<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>
&nbsp;
            HashAlgorithm oHash<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">switch</span><span style="color: #008000;">&#40;</span>hashtype<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><span style="color: #0000FF;">ToUpper</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;MD5&quot;</span><span style="color: #008000;">:</span>
                    oHash <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MD5CryptoServiceProvider<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA&quot;</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA1&quot;</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA128&quot;</span><span style="color: #008000;">:</span>
                    oHash <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SHA1Managed<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA2&quot;</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA256&quot;</span><span style="color: #008000;">:</span>
                    oHash <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SHA256Managed<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA3&quot;</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA384&quot;</span><span style="color: #008000;">:</span>
                    oHash <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SHA384Managed<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA5&quot;</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #666666;">&quot;SHA512&quot;</span><span style="color: #008000;">:</span>
                    oHash <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SHA512Managed<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">:</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> SqlString<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Null</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> hashedBytes <span style="color: #008000;">=</span> oHash<span style="color: #008000;">.</span><span style="color: #0000FF;">ComputeHash</span><span style="color: #008000;">&#40;</span>plainTextBytes<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">string</span> cleanoutput <span style="color: #008000;">=</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BitConverter</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>hashedBytes<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            cleanoutput <span style="color: #008000;">=</span> cleanoutput<span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;-&quot;</span>, <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> SqlString<span style="color: #008000;">&#40;</span>cleanoutput<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Once you have the dll, here is the SQL to create the assembly.  You&#8217;ll need to change the path in the script to that of the dll.  Once the assembly is created, the dll is no longer required.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/*
--You'll need to run this part if CLR is not enabled on your database
EXEC sp_configure 'clr enabled', 1;
RECONFIGURE WITH OVERRIDE;
GO
*/</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @strDllPath <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>
<span style="color: #008080;">--Set this path to the path of the dll</span>
<span style="color: #0000FF;">SET</span> @strDllPath <span style="color: #808080;">=</span> <span style="color: #FF0000;">'C:\cryptohashCLR.dll'</span>
&nbsp;
<span style="color: #008080;">--Drop the assembly if it already exists</span>
<span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">assemblies</span> asms <span style="color: #0000FF;">WHERE</span> asms.<span style="color: #202020;">name</span> <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'cryptohashCLR'</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">DROP</span> ASSEMBLY <span style="color: #808080;">&#91;</span>cryptohashCLR<span style="color: #808080;">&#93;</span>
<span style="color: #008080;">--Create the assembly</span>
<span style="color: #0000FF;">CREATE</span> ASSEMBLY <span style="color: #808080;">&#91;</span>cryptohashCLR<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> @strDllPath <span style="color: #0000FF;">WITH</span> PERMISSION_SET <span style="color: #808080;">=</span> SAFE</pre></div></div>

<p>And then finally, you an now create the function.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">objects</span> <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'[dbo].[udf_hashBytes]'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">AND</span> type in <span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">'FN'</span>, N<span style="color: #FF0000;">'IF'</span>, N<span style="color: #FF0000;">'TF'</span>, N<span style="color: #FF0000;">'FS'</span>, N<span style="color: #FF0000;">'FT'</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">FUNCTION</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>udf_hashBytes<span style="color: #808080;">&#93;</span>
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">FUNCTION</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>udf_hashBytes<span style="color: #808080;">&#93;</span>
	<span style="color: #808080;">&#40;</span>
	@hashtype <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>,
	@<span style="color: #0000FF;">input</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>
	<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">RETURNS</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">EXTERNAL</span> NAME cryptohashCLR.<span style="color: #808080;">&#91;</span>cryptohashCLR.<span style="color: #202020;">SqlHash</span><span style="color: #808080;">&#93;</span>.<span style="color: #202020;">CLRHash</span>
GO
&nbsp;
<span style="color: #008080;">--Execute sample cases</span>
<span style="color: #0000FF;">IF</span> dbo.<span style="color: #202020;">udf_hashbytes</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SHA1'</span>,<span style="color: #FF0000;">'The quick brown fox jumps over the lazy dog'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12'</span>
	<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'Successful: Output of UDF test was as expected'</span>
<span style="color: #0000FF;">ELSE</span>
	<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'Error: Inconsistent results were returned from UDF test'</span></pre></div></div>

<p>The CLR supports the following hash types:</p>
<ul>
<li>MD5</li>
<li>SHA128</li>
<li>SHA256</li>
<li>SHA384</li>
<li>SHA512</li>
</ul>
<p>And is as simple as running:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">PRINT</span> dbo.<span style="color: #202020;">udf_hashbytes</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SHA256'</span>,<span style="color: #FF0000;">'INPUT STRING'</span><span style="color: #808080;">&#41;</span></pre></div></div>

 <p><a href="http://www.john.geek.nz/?flattrss_redirect&amp;id=973&amp;md5=c9f3565eab39b7481575d752c6047127" title="Flattr" target="_blank"><img src="http://www.john.geek.nz/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.john.geek.nz/2009/01/sha256-and-higher-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=johnburns007&amp;popout=1&amp;url=http%3A%2F%2Fwww.john.geek.nz%2F2009%2F01%2Fsha256-and-higher-in-sql-server%2F&amp;language=en_GB&amp;category=text&amp;title=SHA256+and+higher+in+SQL+Server&amp;description=As+part+of+a+new+project+I%26%238217%3Bm+doing%2C+I+needed+to+use+hashes.+A+good+source+of+information+on+hashes+is+available+from+wikipedia.+SQL+has+the+built+in+function...&amp;tags=blog" type="text/html" />
	</item>
	</channel>
</rss>

