<?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>Michael E. Chancey Jr. &#187; String</title>
	<atom:link href="http://michael.chanceyjr.com/tag/string/feed/" rel="self" type="application/rss+xml" />
	<link>http://michael.chanceyjr.com</link>
	<description></description>
	<lastBuildDate>Fri, 20 May 2011 03:43:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Generic License Generation and Validation</title>
		<link>http://michael.chanceyjr.com/useful-code/generic-license-generation-and-validation/</link>
		<comments>http://michael.chanceyjr.com/useful-code/generic-license-generation-and-validation/#comments</comments>
		<pubDate>Fri, 20 May 2011 03:43:18 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[Activate]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Generic]]></category>
		<category><![CDATA[License]]></category>
		<category><![CDATA[License Key]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[SHA256]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/?p=696</guid>
		<description><![CDATA[This was thrown together to see how quickly I could come up with something simple to allow for offline license validation. Up until now I previously used an online system. As well as this works it requires that the user access the internet in order to validate the key (which opens itself to traffic sniffing [...]]]></description>
			<content:encoded><![CDATA[<p>This was thrown together to see how quickly I could come up with something simple to allow for offline license validation.  Up until now I previously used an online system.  As well as this works it requires that the user access the internet in order to validate the key (which opens itself to traffic sniffing and server simulation in order to hack).  This is by no means a perfect system but it should stump people long enough that they will feel better off just buying a key.</p>
<h3>//Code To Generate A License Key</h3>
<hr />
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// GENERATE A UNIQUE KEY THAT CAN BE REVERESED TO VALIDATE WITHOUT INTERNET CONNECTION
/// &lt;/summary&gt;
/// &lt;param name=&quot;AppID&quot;&gt;APPLIATION GUID THE KEY SHOULD BE GERNEATED FOR&lt;/param&gt;
/// &lt;returns&gt;STRING VALUE REPRESENTING A VALID KEY&lt;/returns&gt;
static string GenerateKey(string AppID)
{
    int curSegment = 1;
    string newLicense = &quot;&quot;;
    string License = new string(Guid.NewGuid().ToString().Replace(&quot;-&quot;, &quot;&quot;).ToUpper().ToCharArray().Take(12).ToArray());

    //GENERATE 4 BYTE SEGMENTS WHICH ARE [HASH,HASH][KEY,KEY]
    //VALUES ARE TAKEN FROM THE HASH AT A MATHIMATICALLY COMPUTED LOCATION [(FIRST CHAR ASCII VALUE + SEGMENT) % 32] TO MAKE IT DEPENDENT ON SEGMENT LOCATION AND VALUE
    //THIS SHOULD MAKE IT HARDER TO &quot;GUESS&quot; A KEY OR BRUTE FORCE ONE OUT OF THE SYSTEM
    foreach (Match tmpMatch in Regex.Matches(License, &quot;[a-zA-Z0-9]{2}&quot;))
        newLicense += (newLicense != &quot;&quot; ? &quot;-&quot; : &quot;&quot;) + BitConverter.ToString(SHA256.Create().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(tmpMatch.Value + AppID)).Skip((tmpMatch.Value.ToCharArray()[0] + curSegment++) % 32).Take(1).ToArray()).Replace(&quot;-&quot;, &quot;&quot;) + tmpMatch.Value;

    //RETURN THE NEW LICENSE VALUE TO THE CALLING FUNCTION
    return newLicense;
}
</pre>
<h3>//Code To Validate A License Key</h3>
<hr />
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// VALIDATES A KEY THAT HAS BEEN GENERATED FOR THE GIVEN APPLICATION ID
/// &lt;/summary&gt;
/// &lt;param name=&quot;AppID&quot;&gt;APPLICATION THE KEY SHOULD BE VALIDATED AGAINST&lt;/param&gt;
/// &lt;param name=&quot;Key&quot;&gt;UNIQUE KEY TO COMPARE AGAINST&lt;/param&gt;
/// &lt;returns&gt;TRUE OR FALSE REPRESENTING THE VALIDATION OF THE GIVEN KEY AND APPLICATION ID&lt;/returns&gt;
public static bool ValidateKey(string Key)
{
    try
    {
        int curSegment = 1;
        string[] segments = Key.ToUpper().Split(new char[] { '-' });
        string AppID = ((GuidAttribute)Assembly.GetCallingAssembly().GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value;

        foreach (string segment in segments)
        {
            //SPLIT THE SEGMENT INTO HASH VALUE AND KEY VALUE
            string hashedValue = segment.Substring(0, 2);
            string keyValue = segment.Substring(2, 2);

            //COMPUTE THE HASH USING THE REVERSE ALGORITHM FROM CREATING A HASH
            string hash = BitConverter.ToString(SHA256.Create().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(keyValue + AppID)).Skip((keyValue.ToCharArray()[0] + curSegment++) % 32).Take(1).ToArray());

            //IF THE HASH DOES NOT MATCH WHAT IS BELIEVED SHOULD BE A MATCH THEN DROP OUT BECAUSE THE KEY IS INVALID
            if (hash != hashedValue)
                return false;
        }

        //IF WE MADE IT THIS FAR THEN THE KEY IS VALID
        return true;
    }
    catch
    {
        //IF THERE WAS AN ERROR WHILE ATTEMPTING TO VALIDATE THEN THE KEY MUST BE INVALID
        return false;
    }
}
</pre>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Generic%20License%20Generation%20and%20Validation%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F&amp;title=Generic%20License%20Generation%20and%20Validation&amp;bodytext=This%20was%20thrown%20together%20to%20see%20how%20quickly%20I%20could%20come%20up%20with%20something%20simple%20to%20allow%20for%20offline%20license%20validation.%20%20Up%20until%20now%20I%20previously%20used%20an%20online%20system.%20%20As%20well%20as%20this%20works%20it%20requires%20that%20the%20user%20access%20the%20internet%20in%20order" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F&amp;title=Generic%20License%20Generation%20and%20Validation" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F&amp;title=Generic%20License%20Generation%20and%20Validation&amp;notes=This%20was%20thrown%20together%20to%20see%20how%20quickly%20I%20could%20come%20up%20with%20something%20simple%20to%20allow%20for%20offline%20license%20validation.%20%20Up%20until%20now%20I%20previously%20used%20an%20online%20system.%20%20As%20well%20as%20this%20works%20it%20requires%20that%20the%20user%20access%20the%20internet%20in%20order" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/generic-license-generation-and-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TCP Message Framework</title>
		<link>http://michael.chanceyjr.com/useful-code/tcp-message-framework/</link>
		<comments>http://michael.chanceyjr.com/useful-code/tcp-message-framework/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 03:33:25 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Binary]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Compression]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Extension]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Message]]></category>
		<category><![CDATA[Serialize]]></category>
		<category><![CDATA[Stream]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[TCP]]></category>
		<category><![CDATA[TCP/IP]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/?p=692</guid>
		<description><![CDATA[TCP Message Framework is a simple framework built to make transport of TCP messages via C# a little easier. I have devised a method of sending messages and unpacking them on the other side that requires no additional coding to handle any new message type a programmer might come up with. Need to create a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://michael.chanceyjr.com/site/wp-content/uploads/2010/12/MessageFramework.png"><img src="http://michael.chanceyjr.com/site/wp-content/uploads/2010/12/MessageFramework-150x109.png" alt="" title="MessageFramework" width="150" height="109" class="alignleft size-thumbnail wp-image-689" /></a><br />
TCP Message Framework is a simple framework built to make transport of TCP messages via C# a little easier.  I have devised a method of sending messages and unpacking them on the other side that requires no additional coding to handle any new message type a programmer might come up with.  Need to create a keep-alive packet.  Simply inherit the message class, write the server side and client side code and you are done.  The transport and unwrapping of the message is all handled by the framework.</p>
<p>I used a very similar method when writing my instant messenger client/server.  There is some overhead but when weighed out against it&#8217;s simplcity its very minimal.  If you would like you can download the entire sample project or view the code through this website.<br />
<h3><a href="http://michael.chanceyjr.com/site/wp-content/uploads/2010/12/MessageFramework.zip">Download Sample Project</a> &#8211; <a href="http://michael.chanceyjr.com/free-stuff/tcp-message-framework/">View Code</a></h3>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=TCP%20Message%20Framework%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftcp-message-framework%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftcp-message-framework%2F&amp;title=TCP%20Message%20Framework&amp;bodytext=%0D%0ATCP%20Message%20Framework%20is%20a%20simple%20framework%20built%20to%20make%20transport%20of%20TCP%20messages%20via%20C%23%20a%20little%20easier.%20%20I%20have%20devised%20a%20method%20of%20sending%20messages%20and%20unpacking%20them%20on%20the%20other%20side%20that%20requires%20no%20additional%20coding%20to%20handle%20any%20new%20messa" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftcp-message-framework%2F&amp;title=TCP%20Message%20Framework" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftcp-message-framework%2F&amp;title=TCP%20Message%20Framework&amp;notes=%0D%0ATCP%20Message%20Framework%20is%20a%20simple%20framework%20built%20to%20make%20transport%20of%20TCP%20messages%20via%20C%23%20a%20little%20easier.%20%20I%20have%20devised%20a%20method%20of%20sending%20messages%20and%20unpacking%20them%20on%20the%20other%20side%20that%20requires%20no%20additional%20coding%20to%20handle%20any%20new%20messa" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/tcp-message-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String Truncating Extension</title>
		<link>http://michael.chanceyjr.com/useful-code/string-truncating-extension/</link>
		<comments>http://michael.chanceyjr.com/useful-code/string-truncating-extension/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 10:12:00 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Extension]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[Truncate]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/?p=539</guid>
		<description><![CDATA[This is a simple extension to truncate strings on whole words. If it can not find a whole word to truncate on then it will fall back to breaking a word up. You can also pass a string to append to the end of the truncated string. Other then that there is not a whole [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple extension to truncate strings on whole words.  If it can not find a whole word to truncate on then it will fall back to breaking a word up.  You can also pass a string to append to the end of the truncated string.  Other then that there is not a whole lot to say about this one.</p>
<h3>//Code</h3>
<hr/>
<pre class="brush: csharp;">
        /// &lt;summary&gt;
        /// TRUNCATES A STRING PREFERRING WHOLE WORD OVER NON WHOLE WORD
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;s&quot;&gt;INCOMING STRING TO TRUNCATE&lt;/param&gt;
        /// &lt;param name=&quot;len&quot;&gt;LENGTH IN WHICH TO TRUNCATE TO&lt;/param&gt;
        /// &lt;param name=&quot;postTruncate&quot;&gt;ANYTHING TO ADD TO A TRUNCATED STRING SUCH AS ...&lt;/param&gt;
        /// &lt;returns&gt;TRUNCATED STRING WITH POST TRUNCATE ADDED TO IT&lt;/returns&gt;
        public static string Truncate(this string s, int len, string postTruncate)
        {
            //IF THE STRING IS SMALLER THEN THE LENGTH JUST RETURN THE STRING
            if (s == null || s.Length &lt; len)
                return s;
            else
            {
                //INCASE NULL HAS BEEN PASSED IN CHANGE IT TO EMPTY STRING SO IT HAS A COUNT
                if (postTruncate == null)
                    postTruncate = string.Empty;

                //RETURN THE TRUNCATED STRING BACK TO CALLING FUNCTION
                string tmpReturn = string.Concat(Regex.Match(String.Concat(s, &quot; &quot;), string.Format(&quot;^(?&lt;tmpMatch&gt;.{{0,{0}}})\\s&quot;, len)).Groups[&quot;tmpMatch&quot;].Value, postTruncate);

                //IF THE WHOLE WORD TRUNCATE WORKED THEN USE IT OTHERWISE USE NON WHOLE WORD TRUNCATE
                if (tmpReturn != postTruncate)
                    return tmpReturn;
                else
                    return string.Concat(Regex.Match(String.Concat(s, &quot; &quot;), string.Format(&quot;^(?&lt;tmpMatch&gt;.{{0,{0}}})&quot;, len)).Groups[&quot;tmpMatch&quot;].Value, postTruncate);
            }
        }
</pre>
<h3>//Usage</h3>
<hr/>
<pre class="brush: csharp;">
    class Program
    {
        static void Main(string[] args)
        {
            //OUTPUT WILL BE &quot;Hello, World...&quot;
            Console.WriteLine(&quot;Hello, World this is some long text to truncate&quot;.Truncate(15, &quot;...&quot;));
            Console.ReadLine();
        }
    }
</pre>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=String%20Truncating%20Extension%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F&amp;title=String%20Truncating%20Extension&amp;bodytext=This%20is%20a%20simple%20extension%20to%20truncate%20strings%20on%20whole%20words.%20%20If%20it%20can%20not%20find%20a%20whole%20word%20to%20truncate%20on%20then%20it%20will%20fall%20back%20to%20breaking%20a%20word%20up.%20%20You%20can%20also%20pass%20a%20string%20to%20append%20to%20the%20end%20of%20the%20truncated%20string.%20%20Other%20then%20that%20th" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F&amp;title=String%20Truncating%20Extension" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F&amp;title=String%20Truncating%20Extension&amp;notes=This%20is%20a%20simple%20extension%20to%20truncate%20strings%20on%20whole%20words.%20%20If%20it%20can%20not%20find%20a%20whole%20word%20to%20truncate%20on%20then%20it%20will%20fall%20back%20to%20breaking%20a%20word%20up.%20%20You%20can%20also%20pass%20a%20string%20to%20append%20to%20the%20end%20of%20the%20truncated%20string.%20%20Other%20then%20that%20th" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/string-truncating-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String Compression Extension</title>
		<link>http://michael.chanceyjr.com/useful-code/string-compression-extensions/</link>
		<comments>http://michael.chanceyjr.com/useful-code/string-compression-extensions/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 03:37:42 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Compression]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Extension]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/site/?p=232</guid>
		<description><![CDATA[/// &#60;summary&#62; /// STRING EXTENSION FOR COMPRESSING TO A BASE 64 STRING /// &#60;/summary&#62; /// &#60;param name=&#34;s&#34;&#62;INPUT STRING TO COMPRESS&#60;/param&#62; /// &#60;returns&#62;RETURNS A BASE 64 ENCODED COMPRESSED STRING&#60;/returns&#62; public static string Compress(this string s) { //CREATE A TEMP MEMORY STREAM TO HOLD THE COMPRESSED DATA using (MemoryStream tmpMem = new MemoryStream()) { //CREATE A GZIP [...]]]></description>
			<content:encoded><![CDATA[<pre class="brush: csharp;">
        /// &lt;summary&gt;
        /// STRING EXTENSION FOR COMPRESSING TO A BASE 64 STRING
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;s&quot;&gt;INPUT STRING TO COMPRESS&lt;/param&gt;
        /// &lt;returns&gt;RETURNS A BASE 64 ENCODED COMPRESSED STRING&lt;/returns&gt;
        public static string Compress(this string s)
        {
            //CREATE A TEMP MEMORY STREAM TO HOLD THE COMPRESSED DATA
            using (MemoryStream tmpMem = new MemoryStream())
            {
                //CREATE A GZIP STREAM TO COMPRESS THE INCOMING DATA
                using (GZipStream tmpCompressor = new GZipStream(tmpMem, CompressionMode.Compress))
                {
                    //COMPRESS THE DATA AND CLOSE THE STREAM
                    tmpCompressor.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(s), 0, s.Length);
                    tmpCompressor.Flush();
                    tmpCompressor.Close();
                }

                //ENCODE THE BYTE ARRAY AS A BASE 64 STRING TO REMOVE NULLS AND SUCH
                return Convert.ToBase64String(tmpMem.ToArray());
            }
        }

        /// &lt;summary&gt;
        /// STRING EXTENSION FOR UNCOMPRESSING FROM A BASE 64 STRING
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;s&quot;&gt;BASE 64 STRING WHICH CONTAINS THE COMPRESSED DATA&lt;/param&gt;
        /// &lt;returns&gt;RETURN AN UNCOMPRESSED STRING&lt;/returns&gt;
        public static string UnCompress(this string s)
        {
            //CREATE A TEMP MEMORY STREAM CONTAINING THE COMPRESSED DATA
            using (MemoryStream tmpMem = new MemoryStream(Convert.FromBase64String(s)))
            {
                //CREATE A GZIP STREAM FOR UNCOMPRESSING THE DATA IN THE STREAM
                using (GZipStream tmpCompressed = new GZipStream(tmpMem, CompressionMode.Decompress))
                {
                    byte[] buffer = new byte[1024];
                    int count = 0;
                    StringBuilder tmpReturn = new StringBuilder();

                    //WHILE WE ARE STILL READING DATA KEEP APPENDING IT TO THE STRING BUILDER
                    while ((count = tmpCompressed.Read(buffer, 0, buffer.Length)) &gt; 0)
                    {
                        tmpReturn.Append(System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, count));
                    }

                    //RETURN THE RESULT FROM THE STRING BUILDER APPENDING
                    return tmpReturn.ToString();
                }
            }
        }
</pre>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=String%20Compression%20Extension%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-compression-extensions%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-compression-extensions%2F&amp;title=String%20Compression%20Extension&amp;bodytext=%5Bcsharp%5D%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3Bsummary%26gt%3B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20STRING%20EXTENSION%20FOR%20COMPRESSING%20TO%20A%20BASE%2064%20STRING%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3B%2Fsummary%26gt%3B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3Bparam%20name%3D%26quot%3Bs%26quot%3B%26gt%3BINPUT%20STRING%20TO%20COMPRESS%26lt%3B%2Fparam%26gt%3B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3Breturns%26gt%3BRETU" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-compression-extensions%2F&amp;title=String%20Compression%20Extension" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-compression-extensions%2F&amp;title=String%20Compression%20Extension&amp;notes=%5Bcsharp%5D%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3Bsummary%26gt%3B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20STRING%20EXTENSION%20FOR%20COMPRESSING%20TO%20A%20BASE%2064%20STRING%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3B%2Fsummary%26gt%3B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3Bparam%20name%3D%26quot%3Bs%26quot%3B%26gt%3BINPUT%20STRING%20TO%20COMPRESS%26lt%3B%2Fparam%26gt%3B%0D%0A%20%20%20%20%20%20%20%20%2F%2F%2F%20%26lt%3Breturns%26gt%3BRETU" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/string-compression-extensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

