PDA

Click to See Complete Forum and Search --> : C#-Base64 Encoding and Decoding


Sgt-Peppa
Apr 21st, 2004, 09:26 AM
To encode a string


public string base64Encode(string data)
{
try
{
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch(Exception e)
{
throw new Exception("Error in base64Encode" + e.Message);
}
}


and to decode


public string base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();

byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch(Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}


Hope that saves some time,

Stephan

SugarDaddy420
Aug 25th, 2005, 03:13 PM
No offense, but there's certainly a more elegant solution. I'm not sure why you're catching a specific exception just to throw a more general one either.


public string Encode(string str)
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
return Convert.ToBase64String(encbuff);
}
public string Decode(string str)
{
byte[] decbuff = Convert.FromBase64String(str);
return System.Text.Encoding.UTF8.GetString(decbuff);
}

SugarDaddy420
Sep 1st, 2005, 03:17 PM
Pardon me. I now understand why you were throwing the exception. When an exception occurs within a function, it does not pass it out to the containing function that made the call. You have to throw it back yourself. Nicely done.

axion_sa
Sep 2nd, 2005, 02:04 PM
The following is a better way of bubbling exceptions. It's not entirely necessary in this situation though ;)


try
{
// Code here
}
catch (Exception ex)
{
throw new Exception("Error in base64Decode: " + e.Message, ex);
}


This way, the caller will have access to the original exception information (stack trace et al).

mj12
Mar 25th, 2007, 11:27 PM
To encode a string


public string base64Encode(string data)
{
try
{
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch(Exception e)
{
throw new Exception("Error in base64Encode" + e.Message);
}
}


and to decode


public string base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();

byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch(Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}


Hope that saves some time,

Stephan
Indeed it does Stephan! Short, sweet and right to the meat of it. Thanks for sharing it, m8y!

Chimp
Aug 24th, 2007, 09:39 AM
Pardon me. I now understand why you were throwing the exception. When an exception occurs within a function, it does not pass it out to the containing function that made the call. You have to throw it back yourself. Nicely done.

Yes it does. What do you mean it does not pass it out to the containing function??