CSharp examples for System:String Base64
Decodes the supplied Base-64 encoded string.
// This library is free software; you can redistribute it and/or using System.Text; using System;/* w w w. j a v a 2 s . co m*/ public class Main{ private static readonly string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /// <summary>Decodes the supplied Base-64 encoded string.</summary> /// <remarks>Decodes the supplied Base-64 encoded string.</remarks> /// <param name="string">The Base-64 encoded string that is to be decoded.</param> /// <returns>A <code>byte[]</code> containing the decoded data block.</returns> public static byte[] Decode(string @string) { int length = @string.Length; if (length == 0) { return new byte[0]; } int pad = (@string[length - 2] == '=') ? 2 : (@string[length - 1] == '=') ? 1 : 0; int size = length * 3 / 4 - pad; byte[] buffer = new byte[size]; int block; int i = 0; int index = 0; while (i < length) { block = (Alphabet.IndexOf(@string[i++]) & unchecked(0xff)) << 18 | (Alphabet .IndexOf(@string[i++]) & unchecked(0xff)) << 12 | (Alphabet.IndexOf(@string [i++]) & unchecked(0xff)) << 6 | (Alphabet.IndexOf(@string[i++]) & unchecked( 0xff)); buffer[index++] = unchecked((byte)((int)(((uint)block) >> 16))); if (index < size) { buffer[index++] = unchecked((byte)(((int)(((uint)block) >> 8)) & unchecked(0xff))); } if (index < size) { buffer[index++] = unchecked((byte)(block & unchecked(0xff))); } } return buffer; } }