分配二进制常量

发布于 2024-07-15 05:31:32 字数 529 浏览 5 评论 0原文

有没有办法将二进制值赋给 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 技术交流群。

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

发布评论

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

评论(5

一瞬间的火花 2024-07-22 05:31:32

在这之后我可能会刺伤自己:

Convert.ToInt32("1100101", 2);

更严肃地说(注意到你已经更新了问题),对于标志 enums 你可能想要的是 左移运算符 (<<)

Dim myReality as Integer          = 1 << 0   // 1
Dim myAlternateReality as Integer = 1 << 1   // 2
Dim myParallelUniverse as Integer = 1 << 2   // 4
...

依此类推,最多 31 个。

I might stab myself after this one:

Convert.ToInt32("1100101", 2);

On a more serious note (noticing that you have updated the question), for flag enums what you may want is the left shift operator (<<):

Dim myReality as Integer          = 1 << 0   // 1
Dim myAlternateReality as Integer = 1 << 1   // 2
Dim myParallelUniverse as Integer = 1 << 2   // 4
...

and so on up to 31.

泅人 2024-07-22 05:31:32

不幸的是,你能做的最好的就是十六进制:

Dim x As Long = &H1234ABCD

Unfortunately, the best you can do is Hex:

Dim x As Long = &H1234ABCD
像你 2024-07-22 05:31:32

我想你的意思是约翰·拉什(John Rasch)提供的答案。 您有一个由零和一组成的字符串,您希望将其转换为某种变量。

    Dim b As String = "10101"
    Dim i As Integer = 73
    Dim s As String
    s = Convert.ToString(i, 2) 's contains the binary representation of 73 - 1001001
    i = Convert.ToInt32(b, 2) 'i now =  21

您还可以将这些方法用于十六进制(16)和八进制(8)。

你也可以做这样的事情

    Dim i As Integer = 6
    If (i And _bi.t0) = _bi.t0 OrElse (i And _bi.t1) = _bi.t1 Then
      'bit 0 or 1 on
    End If

Enum _bi As Integer
    t0 = CInt(2 ^ 0)
    t1 = CInt(2 ^ 1)
    t2 = CInt(2 ^ 2)
    t3 = CInt(2 ^ 3)
    t4 = CInt(2 ^ 4)
    t5 = CInt(2 ^ 5)
    t6 = CInt(2 ^ 6)
    t7 = CInt(2 ^ 7)
    t8 = CInt(2 ^ 8)
    t9 = CInt(2 ^ 9)
    t10 = CInt(2 ^ 10)
    t11 = CInt(2 ^ 11)
    t12 = CInt(2 ^ 12)
    t13 = CInt(2 ^ 13)
    t14 = CInt(2 ^ 14)
    t15 = CInt(2 ^ 15)
    t16 = CInt(2 ^ 16)
    t17 = CInt(2 ^ 17)
    t18 = CInt(2 ^ 18)
    t19 = CInt(2 ^ 19)
    t20 = CInt(2 ^ 20)
    t21 = CInt(2 ^ 21)
    t22 = CInt(2 ^ 22)
    t23 = CInt(2 ^ 23)
    t24 = CInt(2 ^ 24)
    t25 = CInt(2 ^ 25)
    t26 = CInt(2 ^ 26)
    t27 = CInt(2 ^ 27)
    t28 = CInt(2 ^ 28)
    t29 = CInt(2 ^ 29)
    t30 = CInt(2 ^ 30)
End Enum

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.

    Dim b As String = "10101"
    Dim i As Integer = 73
    Dim s As String
    s = Convert.ToString(i, 2) 's contains the binary representation of 73 - 1001001
    i = Convert.ToInt32(b, 2) 'i now =  21

and you can also use these methods for hex(16) and octal(8).

you could also do something like this

    Dim i As Integer = 6
    If (i And _bi.t0) = _bi.t0 OrElse (i And _bi.t1) = _bi.t1 Then
      'bit 0 or 1 on
    End If

Enum _bi As Integer
    t0 = CInt(2 ^ 0)
    t1 = CInt(2 ^ 1)
    t2 = CInt(2 ^ 2)
    t3 = CInt(2 ^ 3)
    t4 = CInt(2 ^ 4)
    t5 = CInt(2 ^ 5)
    t6 = CInt(2 ^ 6)
    t7 = CInt(2 ^ 7)
    t8 = CInt(2 ^ 8)
    t9 = CInt(2 ^ 9)
    t10 = CInt(2 ^ 10)
    t11 = CInt(2 ^ 11)
    t12 = CInt(2 ^ 12)
    t13 = CInt(2 ^ 13)
    t14 = CInt(2 ^ 14)
    t15 = CInt(2 ^ 15)
    t16 = CInt(2 ^ 16)
    t17 = CInt(2 ^ 17)
    t18 = CInt(2 ^ 18)
    t19 = CInt(2 ^ 19)
    t20 = CInt(2 ^ 20)
    t21 = CInt(2 ^ 21)
    t22 = CInt(2 ^ 22)
    t23 = CInt(2 ^ 23)
    t24 = CInt(2 ^ 24)
    t25 = CInt(2 ^ 25)
    t26 = CInt(2 ^ 26)
    t27 = CInt(2 ^ 27)
    t28 = CInt(2 ^ 28)
    t29 = CInt(2 ^ 29)
    t30 = CInt(2 ^ 30)
End Enum
臻嫒无言 2024-07-22 05:31:32

或者一些描述性的东西

Enum _SerialPortPins As Integer
    RTS = CInt(2 ^ 4)
    CTS = CInt(2 ^ 5)
    DSR = CInt(2 ^ 6)
    DCD = CInt(2 ^ 8)
    DTR = CInt(2 ^ 20)
    RI = CInt(2 ^ 22)
End Enum

or something descriptive

Enum _SerialPortPins As Integer
    RTS = CInt(2 ^ 4)
    CTS = CInt(2 ^ 5)
    DSR = CInt(2 ^ 6)
    DCD = CInt(2 ^ 8)
    DTR = CInt(2 ^ 20)
    RI = CInt(2 ^ 22)
End Enum
↘紸啶 2024-07-22 05:31:32

使用评论?

Dim myFantasy as Integer = 2^3 '00000000-00001000

Use a comment?

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