Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

.net - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...