<?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>Martijn's C# Programming Blog &#187; base64</title>
	<atom:link href="http://www.dijksterhuis.org/tag/base64/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Encoding and Decoding Binary data using Base64 with C#</title>
		<link>http://www.dijksterhuis.org/encoding-and-decoding-binary-data-using-base64-with-c/</link>
		<comments>http://www.dijksterhuis.org/encoding-and-decoding-binary-data-using-base64-with-c/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 06:36:07 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[convert]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=347</guid>
		<description><![CDATA[It is often necessary to convert binary data to and from strings. One common way of encoding such information is through Base-64 encoding. The following post shows you how to convert a byte array to a Base64 string , and back again. What is Base64? Base-64 encoded information uses the most common characters: A-Z,a-z and [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em>It is often necessary to convert binary data to and from strings. One common way of encoding such information is through Base-64 encoding.  The following post shows you how to convert a byte array to a Base64 string , and back again. </em></p>
<p><span id="more-347"></span></p>
<p><strong>What is Base64?</strong></p>
<p>Base-64 encoded information uses the most common characters: A-Z,a-z and 0-9, allowing for save storage of binary date in places that do not support binary. </p>
<p>The most common situation is  e-mail, but it is also often used to encode binary data in XML files or other text file formats.</p>
<p>An example of Base-64 encoded data:</p>
<blockquote><p>CRc6tbZ+170rwAQ5bPNYXvc8M7cx+xeJiyrFBpda</p></blockquote>
<p><strong>Implementing it in C#</strong></p>
<p>The following example shows how to convert a byte array to a base-64 encoded string and back again. All the actual work is done by the System.Convert class. In the example below we use System.Security.Cryptography to fill our test array with random data.</p>
<pre class="brush: c#">
using System;
using System.Security.Cryptography;

namespace Base64
{
    class MainClass
    {
        public static bool VerifyEqual(byte[] one, byte[] two)
        {
            if (one.Length != two.Length) return false;
            for (int Lp = 0; Lp &lt; one.Length; Lp++)
                if (!one[Lp].Equals(two[Lp])) return false;
            return true;
        }

        public static void Main(string[] args)
        {
            byte[] DataToEncode = new byte[30];

            // Fill the byte array with random data
            RandomNumberGenerator Generator = RandomNumberGenerator.Create();
            Generator.GetNonZeroBytes(DataToEncode);

            // Convert the byte array to a base-64 string
            string Base64String = Convert.ToBase64String(DataToEncode);
            Console.WriteLine(&quot;{0}&quot;,Base64String);

            // Convert the base-64 string back to a byte array
            byte[] DecodedData = Convert.FromBase64String(Base64String);

            // Verify that the data was decoded correctly
            if (!VerifyEqual(DecodedData,DataToEncode))
                Console.WriteLine(&quot;Byte[]&#039;s do not match!&quot;);
            else
                Console.WriteLine(&quot;Byte[]&#039;s match!&quot;);

        }
    }
}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/encoding-and-decoding-binary-data-using-base64-with-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

