c# - 是否有 C# 函数将 64 位 "Unsigned"值格式化为其等效的二进制值?

标签 c# .net binary string-formatting

要将数字格式化/显示为其等效的二进制形式(在 C# 中),我总是简单地调用:

Convert.ToString(myNumber, 2);

今天,我才意识到我一直在调用的 .ToString() 重载不支持大于 9223372036854775807 的值。请注意 .ToString() 重载的签名是:.ToString(long, int) 。其中“long”是一个 64 位有符号值,最大值为 9223372036854775807。

换句话说,使用 C#,当我运行它时:

Convert.ToString(9223372036854775808,2);

我收到此异常消息并不奇怪(由于签名):

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 的值转换为其等效的二进制格式?

最佳答案

你可以称它为无符号或有符号,但按位看是一样的!

所以如果你这样做:

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

如果有 Convert.ToString() 的 ulong 实现,您将获得相同的位,这就是为什么没有……;)

因此,((long)-1)((ulong)-1) 在内存中看起来是一样的。

关于c# - 是否有 C# 函数将 64 位 "Unsigned"值格式化为其等效的二进制值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6985965/

相关文章:

c# - 编写一个函数来自动将数字排序为 256 个单元的 block ?

c# - ASP.NET MVC4 : An attribute argument must be a constant expression , 属性参数类型的typeof表达式或数组创建表达式

c# - 等到我的线程结束

linux - native GCC 5 二进制文件太大 (libexec)

c# - C# 'out' 或 'ref' 参数的值何时实际返回给调用方?

c# - 如何使用 "Resources embedded in resx"的资源?

c# - WiX 安装程序 : Can one msi have - one windows app and one windows service using WiX

数据库索引和查找与 "closest neighbour"不完全匹配

perl - 在 Perl 中将位向量转换为二进制

c# - 自定义 DataGridView 重复列