Base64 from http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java : Base64 « Development Class « C# / C Sharp






Base64 from http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java

 
using System;
using System.Collections;

public static class HTTPUtility
{


    #region Base64

    // Adapted from example at: http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java
    public static string ConvertToBase64String(byte[] inArray)
    {
        // Base 64 character set
        const string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

        // Result and padding strings
        string r = string.Empty;
        string p = string.Empty;

        // Zero pad the array if necessary
        int c = inArray.Length % 3;
        if (c > 0)
        {
            int addedChars = 3 - c;
            p = new string('=', addedChars);
            byte[] newArray = new byte[inArray.Length + addedChars];
            inArray.CopyTo(newArray, 0);
            inArray = newArray;
        }

        // Convert the input array
        for (int i = 0; i < inArray.Length; i += 3)
        {
            // Add a newline character if necessary
            if (i > 0 && (i / 3 * 4) % 76 == 0)
                r += "\r\n";

            // Three bytes become one 24-bit number
            int n = (inArray[i] << 16) + (inArray[i + 1] << 8) + (inArray[i + 2]);

            // 24-bit number gets split into four 6-bit numbers
            int n1 = (n >> 18) & 63;
            int n2 = (n >> 12) & 63;
            int n3 = (n >> 6) & 63;
            int n4 = n & 63;

            // Use the four 6-bit numbers as indices for the base64 character list
            r = string.Concat(r, base64Chars[n1], base64Chars[n2], base64Chars[n3], base64Chars[n4]);
        }

        return r.Substring(0, r.Length - p.Length) + p;
    }

    #endregion
}

   
  








Related examples in the same category

1.Double To Int 64 Bits, Int 64 Bits To Double
2.Base 64 encode
3.Base64 Encoder
4.Url Encode Base 64
5.The Base64 utility class performs base64 encoding and decoding.
6.The Base64 utility class performs base64 encoding and decoding. The resulting binary data is returned as an array of bytes.
7.Decodes all or part of the input base64 encoded StringBuffer
8.Encode To 64
9.Encrypt Key and Decrypt Key with Convert.ToBase64String and Convert.FromBase64String
10.Base64 Serializer
11.Serializes and deserializes an entity to a base 64 string
12.Convert To Base 64 String
13.Get Xml value as Integer 64
14.Create a hex string from an Int64 value