Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 

A C# Implementation of Mime De/encode

By , 21 Aug 2005
 
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 use 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)

About the Author

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 22 Aug 2005
Article Copyright 2005 by lionwind
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid