Tuesday, May 25, 2021

Base64 - Encoding and Decoding s String

The following class can be used to encode a string or decode an encoded string. 


   public static class Base64  
   {  
     public static string Encode(this System.Text.Encoding encoding, string textValue)  
     {  
       if (textValue == null)  
       {  
         return null;  
       }  
       try  
       {  
         byte[] textAsBytes = encoding.GetBytes(textValue);  
         return System.Convert.ToBase64String(textAsBytes);  
       }  
       catch (Exception)  
       {  
         return textValue;  
       }        
     }  
     public static string Decode(this System.Text.Encoding encoding, string encodedTextValue)  
     {  
       if (encodedTextValue == null)  
       {  
         return null;  
       }  
       try  
       {  
         byte[] textAsBytes = System.Convert.FromBase64String(encodedTextValue);  
         return encoding.GetString(textAsBytes);  
       }  
       catch (Exception)  
       {  
         return encodedTextValue;  
       }        
     }  
   }  

So, you can refer this as below;



 EncodingForBase64.DecodeBase64(Encoding.UTF8, yourtextvalue);  

Happy coding...

No comments:

Post a Comment