Take the 2-minute tour ×

To format/display a number to its equivalent binary form (in C#), I have always simply called:

Convert.ToString(myNumber, 2);

Today, I just realized that the .ToString() overload that I have been calling does not support values that are greater than 9223372036854775807. Note the .ToString() overload's signature is: .ToString(long, int). Where "long" is a 64bit signed value which max's out at 9223372036854775807.

To put it another way, using C#, when I run this:


It's no surprise (due to the signature) that I receive this exception message:

The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments - Argument 2: cannot convert from 'int' to 'System.IFormatProvider'

My question: Is there a .NET function that allows us to convert values greater than 9223372036854775807 to their equivalent binary format?

share|improve this question
What exact format do you mean? Big-endian base-2? –  Marc Gravell Aug 8 '11 at 17:27
Little-endian base2 will do. –  Jed Aug 8 '11 at 19:13
add comment

2 Answers

up vote 12 down vote accepted

You can call it unsigned or signed, but its the same if you look at it bitwise!

So if you do this:


you would get the same bits as you would if there were ulong implementation of Convert.ToString(), and thats why there is none... ;)

Therefore, ((long)-1) and ((ulong)-1) looks the same in memory.

share|improve this answer
add comment

Unfortunately there's no direct .NET equivalent like Convert.ToString(ulong, int). You'll have to make your own, like the following:

public static string ConvertToBinary(ulong value){
  if(value==0)return "0";
  System.Text.StringBuilder b=new System.Text.StringBuilder();
    b.Insert(0,((value&1)==1) ? '1' : '0');
  return b.ToString();
share|improve this answer
add comment

Your Answer


