Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

A C# Implementation of Mime De/encode

, 21 Aug 2005 CPOL
A C# implementation of Mime de/encode
using System;
using System.IO;
using System.Text;

namespace MIME
{
	/// <summary>
	/// 
	/// </summary>
	public class MimeCodeBase64 : MIME.MimeCode
	{
		public MimeCodeBase64()
		{
			// 
			// TODO: Add constructor logic here
			//
		}

		public override byte[] DecodeToBytes(string s)
		{
			if(s == null)
				throw new ArgumentNullException();

			return System.Convert.FromBase64String(s);
		}

		public override string EncodeFromBytes(byte[] inArray, int offset, int length)
		{
			if(inArray == null)
				throw new ArgumentNullException();

			return FormatEncodedString(System.Convert.ToBase64String(inArray, offset, length));
		}

		string FormatEncodedString(string s)
		{
			if(s == null)
				throw new ArgumentNullException();

			const int MAX_CHAR_LEN = 76;
			int index = 0;
			StringBuilder sb = new StringBuilder();
			while((index+MAX_CHAR_LEN) < s.Length)
			{
				sb.AppendFormat("{0}\r\n", s.Substring(index, MAX_CHAR_LEN));
				index += MAX_CHAR_LEN;
			}
			sb.AppendFormat("{0}", s.Substring(index, s.Length - index));
			return sb.ToString();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

tauyoung
Web Developer
China China
I think I have to start coding my life now...Smile | :)

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 22 Aug 2005
Article Copyright 2005 by tauyoung
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid