Base64 Decoding

K

Kuldeep

Hi All,

Could you please give me some guidelines on dealing with
Base64 encoded string.
The actual purpose is to decode Base64 Encoded string
and stream the data to a browser so that it can be viewed
in a web application

Regards,
Kuldeep
 
S

Siva M

Not sure what exactly you are looking for. If you are finding a way to
decode a base64 string, use Convert.FromBase64String()

Hi All,

Could you please give me some guidelines on dealing with
Base64 encoded string.
The actual purpose is to decode Base64 Encoded string
and stream the data to a browser so that it can be viewed
in a web application

Regards,
Kuldeep
 
Joined
Aug 29, 2006
Messages
8
Reaction score
0
Base64Decode function

Here is a simple function to decode a base64 encoded string:

Code:
    public static string Base64Decode(string data)
    {
        try
        {
            // Decode the base 64 string into a decoded byte array
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            System.Text.Decoder utf8Decode = encoder.GetDecoder();
            byte[] todecode_byte = Convert.FromBase64String(data);

            // Calculate the number of characters in the string
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

            // Convert the byte array to a char array
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

            // Convert the char array into a string
            string result = new String(decoded_char);
            return result;
        }
        catch (Exception e)
        {
            throw new Exception("Error in base64Decode" + e.Message);
        }
    }


I hope this is what you are looking for.

Gary Francis
Software Developer
Providing UK IT Support
 
G

Guest

public static string Base64Encode(string data) {
byte[] myBytes = Encoding.ASCII.GetBytes(data.Trim().ToCharArray());
return Convert.ToBase64String (myBytes);
}

public static string Base64Decode(string data) {
try {
return Encoding.ASCII.GetString (Convert.FromBase64String (data.Trim()));
} catch {
// Invalid String!
return "";
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top