Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# function that formats a 64bit "Unsigned" value to its equivalent binary value?

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:

Convert.ToString(9223372036854775808,2);

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?

like image 684
Jed Avatar asked Aug 08 '11 17:08

Jed


People also ask

Why does AC have a slash?

Why is there a slash between A and C in A/C? AC is used as an abbreviation for alternating current and A/C for air conditioning. For most people AC is used for alternating current because it was the first use of this abbreviation and A/C is used for air conditioning to differentiate from alternating current.

Why is it called AC?

He combined moisture with ventilation to "condition" and change the air in the factories, controlling the humidity so necessary in textile plants. Willis Carrier adopted the term and incorporated it into the name of his company. Domestic air conditioning soon took off.

Which is correct AC or AC?

Senior Member. A/C unit (air conditioning unit) is a single machine. (e.g. What's that ugly box on your wall? - It's the air conditioning unit.) A/C (air conditioning) is the entire system, or the result it gives.

What is AC?

What A/C Means. The term “A/C” stands for “air conditioning,” but it's frequently used to describe any type of home cooling equipment, such as a traditional split-system air conditioner or heat pump, mini-split unit, geothermal system, or even a window unit.


2 Answers

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

So if you do this:

Convert.ToString((long)myNumber,2);

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.

like image 176
Cipi Avatar answered Oct 06 '22 03:10

Cipi


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();
  while(value!=0){
    b.Insert(0,((value&1)==1) ? '1' : '0');
    value>>=1;
  }
  return b.ToString();
}
like image 36
Peter O. Avatar answered Oct 06 '22 02:10

Peter O.