To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > VBForums CodeBank > CodeBank - C#

Reply Post New Thread
 
Thread Tools Display Modes
Old Apr 21st, 2004, 08:26 AM   #1
Sgt-Peppa
Hyperactive Member
 
Sgt-Peppa's Avatar
 
Join Date: Mar 03
Location: Munich - Germany
Posts: 476
Sgt-Peppa  is on a distinguished road (40+)
C#-Base64 Encoding and Decoding

To encode a string

Code:
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

Code:
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
__________________
Keep Smiling - even if its hard
Frankie Says Relax, wossname Says Yeah!
wossname:--Currently I'm wearing a gimp suit and a parachute.
C# - Base64 Blog
Sgt-Peppa is offline   Reply With Quote
Old Aug 25th, 2005, 02:13 PM   #2
SugarDaddy420
New Member
 
Join Date: Aug 05
Posts: 2
SugarDaddy420 is an unknown quantity at this point (<10)
Re: C#-Base64 Encoding and Decoding

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.

Code:
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 is offline   Reply With Quote
Old Sep 1st, 2005, 02:17 PM   #3
SugarDaddy420
New Member
 
Join Date: Aug 05
Posts: 2
SugarDaddy420 is an unknown quantity at this point (<10)
Re: C#-Base64 Encoding and Decoding

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.
SugarDaddy420 is offline   Reply With Quote
Old Sep 2nd, 2005, 01:04 PM   #4
axion_sa
Frenzied Member
 
axion_sa's Avatar
 
Join Date: Jan 02
Location: Joburg, RSA
Posts: 1,722
axion_sa is a jewel in the rough (200+)axion_sa is a jewel in the rough (200+)axion_sa is a jewel in the rough (200+)
Re: C#-Base64 Encoding and Decoding

The following is a better way of bubbling exceptions. It's not entirely necessary in this situation though

Code:
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).
axion_sa is offline   Reply With Quote
Old Mar 25th, 2007, 10:27 PM   #5
mj12
New Member
 
Join Date: Mar 07
Posts: 1
mj12 is an unknown quantity at this point (<10)
Thumbs up Re: C#-Base64 Encoding and Decoding

Quote:
Originally Posted by Sgt-Peppa
To encode a string

Code:
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

Code:
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!
mj12 is offline   Reply With Quote
Old Aug 24th, 2007, 08:39 AM   #6
Chimp
New Member
 
Join Date: Aug 07
Posts: 1
Chimp is an unknown quantity at this point (<10)
Re: C#-Base64 Encoding and Decoding

Quote:
Originally Posted by SugarDaddy420
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??
Chimp is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - C#


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:54 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.