SAF(NET) = STEPHEN A. FUQUA operating on the Web since 1995

Stephen is a web developer, Bahá'í, and interfaith activist in St. Paul, Minnesota. He likes to write about religion, social justice, sustainability, science, programming, &c.

January 11, 2008

Viewing Embedded Images in HTML E-mail (Base64)

Problem: you have image data from an HTML e-mail, but don't know what to do with it. When you save it to disk it comes out with a weird string instead of binary data.

Solution: pretty simple, you just have to know the terms. This string of data is actually Base64 encoded, which means that the binary data has been converted to a text string for easy transmission over text protocols. .Net Developer's Journal has a good full explanation. All you have to do is convert the string over to a byte array using Convert.FromBase64String, then save your new byte[].

And what if the string is itself transmitted as a byte array? Well, simply convert each byte into a char and then convert the resulting array into the new byte array:

// Convert "string" to chars
byte[] byteEncodedString = ...;
char[] charBytes = new char[byteEncodedString.Length];
for (int i = 0; i < byteEncodedString.Length; i++)
     charBytes[i] = Convert.ToChar(byteEncodedString[i]);

// Convert chars back to bytes
byte[] newBytes = Convert.FromBase64CharAray(charBytes, 0, charBytes.Length);

// Save
using (FileStream fs = new FileStream(mypath, FileMode.Create))
     fs.Write(newBytes, 0, newBytes.Length);

TrackBack

Comments

Post a comment

Remember personal info?




deprecated

On safnet.com

Other sites managed or developed by S.A.F.

S.A.F. elsewhere on the web

  • LinkedIn
    LinkedIn can actually be useful when looking for prospective hires and business or organizational partners
  • GoodReads
    A fun and relatively-unknown social networking site geared towards one's book list
  • Live Journal
    Mirror of the blog at safnet.com, so that a few LJ friends can more easily read and comment there