My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
C# CODE EXAMPLE created on Wednesday, June 30, 2010 permalink
How to encode binary files to text files and back to binary again
This shows you how you can take e.g. png, jpg, mp3 files and convert them to text, for example if you want to transport them via plain text, and then on the other site turn them back into their original binary format.
using System;
using System.IO;
using System.Text;

namespace TestEnc23833
{
    class Program
    {
        static void Main(string[] args)
        {

            FileHelpers.EncodeBinaryFileToTextFile(@"C:testenctest.png", @"C:testenctest.txt");
            FileHelpers.DecodeTextFileFromBinaryFile(@"C:testenctest.txt", @"C:testenctestcopy.png");

            Console.WriteLine("finished");
            Console.ReadLine();
        }
    }

    public static class FileHelpers
    {
        public static void EncodeBinaryFileToTextFile(string inputFileName, string outputFileName)
        {
            FileStream inFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read);
            byte[] binaryData = new Byte[inFile.Length];
            long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
            inFile.Close();
            string base64String = Convert.ToBase64String(binaryData, 0, binaryData.Length);
            StreamWriter outFile = new StreamWriter(outputFileName, false, Encoding.ASCII);
            outFile.Write(base64String);
            outFile.Close();
        }

        public static void DecodeTextFileFromBinaryFile(string inputFileName, string outputFileName)
        {
            StreamReader inFile = new StreamReader(inputFileName, Encoding.ASCII);
            char[] base64CharArray = new char[inFile.BaseStream.Length];
            inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
            string base64String = new string(base64CharArray);
            byte[] binaryData = Convert.FromBase64String(base64String);
            FileStream outFile = new FileStream(outputFileName, FileMode.Create, FileAccess.Write);
            outFile.Write(binaryData, 0, binaryData.Length);
            outFile.Close();
        }
    }
}
need markup?