问题描述
要将数字格式化/显示为其等效的二进制形式(在 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设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)