VB.NET 十六进制中的尾随 & 符号?

发布于 2024-11-25 08:46:53 字数 360 浏览 0 评论 0原文

这对人们来说应该是一件容易的事。谷歌除了链接到一篇简介的内容农场之外什么也没有,而且那是用蹩脚的英语写的。所以,让我们把它清理干净,让它永远埋葬在那里。

VB 十六进制数字尾随的 & 符号有什么用?我读过它强制转换为 Int32,以防 VB 想要尝试存储为 Int16。这对我来说很有意义。但我没有从简介中得到的部分是始终使用尾随的 & 符号来表示位掩码、标志、枚举等。显然,这与重写 VB 对内部使用带符号数字的迷恋有关,这可能会导致比较结果很奇怪。

那么为了简单起见,VB.Net 十六进制数字的规则是什么,带或不带尾随 & 符号?请包括位掩码/标志等情况下的具体用法,以及如何使用它来强制签名与未签名。

请不要使用 C# :)

This should be an easy one for folks. Google's got nothing except content farms linking to one blurb, and that's written in broken English. So let's get this cleared up here where it'll be entombed for all time.

What's the trailing ampersand on VB hexadecimal numbers for? I've read it forces conversion to an Int32 on the chance VB wants to try and store as an Int16. That makes sense to me. But the part I didn't get from the blurb was to always use the trailing ampersand for bitmasks, flags, enums, etc. Apparantly, it has something to do with overriding VB's fetish for using signed numbers for things internally, which can lead to weird results in comparisons.

So to get easy points, what are the rules for VB.Net hexadecimal numbers, with and without the trailing ampersand? Please include the specific usage in the case of bitmasks/flags and such, and how one would also use it to force signed vs. unsigned.

No C# please :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆° 2024-12-02 08:46:53

Vb.net 会将 0x80000000-0xFFFFFFFF 范围内的“&h”表示法十六进制常量视为负数,除非类型明确指定为 UInt32、Int64 或 UInt64。如果数字是用“&”后面精确的八位数字书写的,这种行为可能是可以理解的,但由于某种原因我无法理解,即使数字是用前导零书写的,vb.net也会这样做我>.在当前版本的 VB 中,可以通过使用后缀“&”来强制正确计算数字。后缀 (Int64)、“L”(Int64)、“UL”(UInt64) 或“UI”(UInt32)。在 VB 的早期版本中,“问题范围”是 0x8000-0xFFFF,强制正确计算该范围内的数字(作为 32 位整数,当时称为“Long”)的唯一方法是尾随和符号。

Vb.net will regard "&h"-notation hex constants in the range from 0x80000000-0xFFFFFFFF as negative numbers unless the type is explicitly specified as UInt32, Int64, or UInt64. Such behavior might be understandable if the numbers were written with precisely eight digits following the "&", but for some reason I cannot fathom, vb.net will behave that way even if the numbers are written with leading zeroes. In present versions of VB, one may force the number to be evaluated correctly by using a suffix of "&" suffix (Int64), "L" (Int64), "UL" (UInt64), or "UI" (UInt32). In earlier versions of VB, the "problem range" was 0x8000-0xFFFF, and the only way to force numbers in that range to be evaluated correctly (as a 32-bit integer, which was then called a "Long") was a trailing ampersand.

温柔戏命师 2024-12-02 08:46:53

Visual Basic 具有键入字符。这些可用于修改变量声明和文字,尽管我不建议在变量声明中使用它们 - 如今大多数开发人员更熟悉 As。例如,以下声明是等效的:

Dim X&
Dim X As Long

但就我个人而言,我发现第二个更具可读性。如果我看到第一个,我实际上必须访问上面的链接,或者使用智能感知来找出变量是什么(如果查看纸上的代码则不好)。

Visual Basic has the concept of Type Characters. These can be used to modify variable declarations and literals, although I'd not recommend using them in variable declarations - most developers are more familiar these days with As. E.g. the following declarations are equivalent:

Dim X&
Dim X As Long

But personally, I find the second more readable. If I saw the first, I'd actually have to go visit the link above, or use Intellisense, to work out what the variable is (not good if looking at the code on paper).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文