Decode Base64 with Encoding - CSharp System

CSharp examples for System:String Base64

Description

Decode Base64 with Encoding

Demo Code


using System.Text;
using System;//from   w w  w .  ja  v  a2 s.com

public class Main{

        public static string DecodeBase64(string result)
        {
            int modeX = result.Length % 4;
            if (modeX != 0)
            {
                for (int i = 0; i < 4 - modeX; i++)
                {
                    result = result + "=";
                }
            }
            return DecodeBase64(Encoding.UTF8, result);
        }

        public static string DecodeBase64(Encoding encoding, string result)
        {
            string decode;
            byte[] bytes = Convert.FromBase64String(result);

            try
            {
                decode = encoding.GetString(bytes);
            }
            catch
            {
                decode = result;
            }
            return decode;
        }
}

Related Tutorials