分配二进制常量
有没有办法将二进制值赋给 VB 变量? 所有显而易见的选择都不起作用。 我尝试使用 &B
前缀,附加 b
但似乎没有任何效果。 我寻找它的运气也不好。 我的应用程序不需要这个,但我只是好奇,所以不需要替代解决方案。
[编辑] 为了澄清起见,我正在寻找(这似乎不可能)是如何将文字二进制数分配给变量,以类似于分配十六进制或八进制数的方式。 我只是在寻找一种更具视觉吸引力的方式来为标志枚举分配值。
代码:
Dim num as Integer = &H100ABC 'Hex'
Dim num2 as Integer = &O123765 'Octal'
Dim myFantasy as Integer = &B1000 'What I would like to be able to do'
Dim myReality as Integer = 2 ^ 3 'What I ended up doing'
Is there a way to assign a binary value to a VB variable? All of the obvious choices don't work. I've tried prefixing with &B
, appending with b
but nothing seems to work. I'm not having much luck searching for it either. I don't need this for my application, but I'm rather, just curious so alternate solutions are not necessary.
[Edit]
For clarification, what I was looking for (which does not appear to be possible) was how to assign a literal binary number to a variable, in a similar manner in which you can assign a hex or octal number. I was just looking for a more visually engaging way to assign values to a flag enum.
Code:
Dim num as Integer = &H100ABC 'Hex'
Dim num2 as Integer = &O123765 'Octal'
Dim myFantasy as Integer = &B1000 'What I would like to be able to do'
Dim myReality as Integer = 2 ^ 3 'What I ended up doing'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在这之后我可能会刺伤自己:
更严肃地说(注意到你已经更新了问题),对于标志
enums
你可能想要的是 左移运算符 (<<
):依此类推,最多 31 个。
I might stab myself after this one:
On a more serious note (noticing that you have updated the question), for flag
enums
what you may want is the left shift operator (<<
):and so on up to 31.
不幸的是,你能做的最好的就是十六进制:
Unfortunately, the best you can do is Hex:
我想你的意思是约翰·拉什(John Rasch)提供的答案。 您有一个由零和一组成的字符串,您希望将其转换为某种变量。
您还可以将这些方法用于十六进制(16)和八进制(8)。
你也可以做这样的事情
I assume you mean what John Rasch provided an answer for. You have a string composed of zeroes and ones that you want converted to some kind of variable.
and you can also use these methods for hex(16) and octal(8).
you could also do something like this
或者一些描述性的东西
or something descriptive
使用评论?
Use a comment?