Tuesday, November 02, 2010

Convert Int64 to Binary

I had a situation where I needed to convert an Int64 to a Sql.data.linq.binary and just could not find any valid posts on the net for how to make it happen.  Those that I did find had mistakes in their implementation and either didn't compile or were converting to a base64 which is out of this universe wrong-headed.

I solved it by combining combining two wrong answers to make a right answer.  (And they say that "two wrongs don't make a right" but I've heard that three left turns do!)  Here's the real way to do it...

System.Data.Linq.Binary myBinaryVar = null;

myBinaryVar = new System.Text.ASCIIEncoding().GetBytes(Convert.ToString(myInt64Val,2));