<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Convert Binary to Base64 String</title>
	<atom:link href="http://www.csharp411.com/convert-binary-to-base64-string/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharp411.com/convert-binary-to-base64-string/</link>
	<description>C# Information, Code, Tips and News</description>
	<lastBuildDate>Fri, 12 Mar 2010 07:55:46 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Damian</title>
		<link>http://www.csharp411.com/convert-binary-to-base64-string/comment-page-1/#comment-5266</link>
		<dc:creator>Damian</dc:creator>
		<pubDate>Tue, 25 Aug 2009 08:48:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.mini-tools.com/at2/csharp/wordpress/convert-binary-to-base64-string/#comment-5266</guid>
		<description>Hi - This is a gret post that I have found extremely helpful.

I am using Base 64 to create shorter URLs as described, and I&#039;m not too familiar with all the maths of bytes and bases.

Public Shared Function encUC(ByVal uniqueCode As String) As String

        Dim val As Int64 = Convert.ToInt64(uniqueCode)
        Dim retVal As String

        Dim tinyByte() = System.BitConverter.GetBytes(val)

        &#039;NB THIS WILL WORK FOR A MAX OF 12 DIGIT CODES
        &#039;ReDim Preserve tinyByte(4)

        Dim tinyCode = Base64.Base64Encoder.ToBase64(tinyByte)

        retVal = Replace(tinyCode, &quot;=&quot;, &quot;_&quot;)
        retVal = Replace(retVal, &quot;/&quot;, &quot;-&quot;)
        retVal = Replace(retVal, &quot;+&quot;, &quot;&#124;&quot;)

        Return retVal

    End Function

When I encode it seems to not give me the least possible characters in the base64 string

eg 

1000001728 encodes as wNCaOwAAAAA_

So i have shortened the byte array :
ReDim Preserve tinyByte(4)

Which is fine as my codes are a max of 12 digits, but not very elegant.

Can you suggest anything?

ALSO

When I decode a 32 bit integer (8 digit code)

Public Shared Function decUC(ByVal encCode As String) As String

        Dim repCode As String = encCode
        repCode = Replace(repCode, &quot;_&quot;, &quot;=&quot;)
        repCode = Replace(repCode, &quot;-&quot;, &quot;/&quot;)
        repCode = Replace(repCode, &quot;&#124;&quot;, &quot;+&quot;)

        Dim origByte(7) As Byte
        origByte = Base64.Base64Encoder.FromBase64(repCode)
        ReDim Preserve origByte(7)

        Return System.BitConverter.ToInt64(origByte, 0)

    End Function

 if I don&#039;t use ReDim Preserve origByte(7) it gives me an exception on the 

Return System.BitConverter.ToInt64(origByte, 0) (array to short)

As I won&#039;t always know if i&#039;m getting an 8 or 12 digital code - I want my functions to cater for both. Any ideas?

My thanks again for a really great post that ended hours of midnight hunting  :)</description>
		<content:encoded><![CDATA[<p>Hi &#8211; This is a gret post that I have found extremely helpful.</p>
<p>I am using Base 64 to create shorter URLs as described, and I'm not too familiar with all the maths of bytes and bases.</p>
<p>Public Shared Function encUC(ByVal uniqueCode As String) As String</p>
<p>        Dim val As Int64 = Convert.ToInt64(uniqueCode)<br />
        Dim retVal As String</p>
<p>        Dim tinyByte() = System.BitConverter.GetBytes(val)</p>
<p>        'NB THIS WILL WORK FOR A MAX OF 12 DIGIT CODES<br />
        'ReDim Preserve tinyByte(4)</p>
<p>        Dim tinyCode = Base64.Base64Encoder.ToBase64(tinyByte)</p>
<p>        retVal = Replace(tinyCode, "=", "_")<br />
        retVal = Replace(retVal, "/", "-")<br />
        retVal = Replace(retVal, "+", "|")</p>
<p>        Return retVal</p>
<p>    End Function</p>
<p>When I encode it seems to not give me the least possible characters in the base64 string</p>
<p>eg </p>
<p>1000001728 encodes as wNCaOwAAAAA_</p>
<p>So i have shortened the byte array :<br />
ReDim Preserve tinyByte(4)</p>
<p>Which is fine as my codes are a max of 12 digits, but not very elegant.</p>
<p>Can you suggest anything?</p>
<p>ALSO</p>
<p>When I decode a 32 bit integer (8 digit code)</p>
<p>Public Shared Function decUC(ByVal encCode As String) As String</p>
<p>        Dim repCode As String = encCode<br />
        repCode = Replace(repCode, "_", "=")<br />
        repCode = Replace(repCode, "-", "/")<br />
        repCode = Replace(repCode, "|", "+")</p>
<p>        Dim origByte(7) As Byte<br />
        origByte = Base64.Base64Encoder.FromBase64(repCode)<br />
        ReDim Preserve origByte(7)</p>
<p>        Return System.BitConverter.ToInt64(origByte, 0)</p>
<p>    End Function</p>
<p> if I don't use ReDim Preserve origByte(7) it gives me an exception on the </p>
<p>Return System.BitConverter.ToInt64(origByte, 0) (array to short)</p>
<p>As I won't always know if i'm getting an 8 or 12 digital code &#8211; I want my functions to cater for both. Any ideas?</p>
<p>My thanks again for a really great post that ended hours of midnight hunting  <img src='http://www.csharp411.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
