要将数字格式化/显示为其等效的二进制形式(在 C# 中),我总是简单地调用:
To format/display a number to its equivalent binary form (in C#), I have always simply called:
Convert.ToString(myNumber, 2);
今天,我刚刚意识到我一直在调用的 .ToString() 重载不支持大于 9223372036854775807 的值.注意 .ToString() 重载的签名是:.ToString(long, int)代码>.其中long"是一个 64 位 signed 值,最大值为 9223372036854775807.
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.
换句话说,使用 C#,当我运行它时:
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:
'System.Convert.ToString(object,System.IFormatProvider)' 有一些无效的参数- 参数 2:无法从 'int' 转换为 'System.IFormatProvider'
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'
我的问题:是否有一个 .NET 函数允许我们将大于 9223372036854775807 的值转换为等效的二进制格式?
My question: Is there a .NET function that allows us to convert values greater than 9223372036854775807 to their equivalent binary format?
你可以称它为无符号或有符号,但按位看是一样的!
You can call it unsigned or signed, but its the same if you look at it bitwise!
如果你这样做:
Convert.ToString((long)myNumber,2);
如果有 Convert.ToString() 的 ulong 实现,您将获得相同的位,这就是为什么没有 ... ;)
you would get the same bits as you would if there were ulong implementation of Convert.ToString(), and thats why there is none... ;)
因此,((long)-1)
和 ((ulong)-1)
在内存中看起来是一样的.
Therefore, ((long)-1)
and ((ulong)-1)
looks the same in memory.
这篇关于是否有格式化 64 位“无符号"的 C# 函数?值与其等效的二进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!