是否有格式化 64 位“无符号"的 C# 函数?值与其等效的二进制值?

编程入门 行业动态 更新时间:2024-10-24 02:33:28
本文介绍了是否有格式化 64 位“无符号"的 C# 函数?值与其等效的二进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

要将数字格式化/显示为其等效的二进制形式(在 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# 函数?值与其等效的二进制值?

本文发布于:2023-11-08 15:51:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   符号   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!