【问题标题】:VB.net Sbyte to Byte poroblemVB.net Sbyte 到 Byte 问题
【发布时间】:2016-01-07 11:41:55
【问题描述】:

希望有人可以帮助我。

我有以下代码返回我从这里获得的 IP 地址:

https://www.novell.com/communities/coolsolutions/cool_tools/reading-ldap-networkaddress-attribute-server-or-user-object/

在 C# 中完美运行(代码如下)

LdapAttribute serverNetAddr = entry.getAttribute("networkaddress");

// loop through the multivalued networkaddress field
foreach (sbyte[] addrBytes in serverNetAddr.ByteValueArray)
{

    // get the first character in the line which indicates type
    char type = (char)addrBytes[0];

    if (type == '9')
    { // only interested in TCP address
        string serverTCPAddr = "";
        for (int i = (addrBytes.Length - 4); i < addrBytes.Length; i++)
        { // last four bytes are the ip address
            byte b = unchecked((byte)addrBytes[i]); // convert sbyte to byte
            serverTCPAddr = serverTCPAddr + b; // append value to string
            if (i > 0 && i < (addrBytes.Length - 1))
                serverTCPAddr = serverTCPAddr + "."; // brute force the dots
            textBox1.Text = serverTCPAddr;
        }

    }

}

当我尝试在 VB.net 中使用代码时(见下面的代码)它只能工作到“For i As Integer = (addrBytes.Length - 4) To addrBytes.Length – 1”当代码到达“Dim b As Byte = unchecked(CByte(addrBytes(i)))”然后我收到以下错误消息:

" System.OverflowException:算术运算导致 溢出。”

    Dim serverNetAddr As LdapAttribute = entry.getAttribute("networkaddress")



    ' loop through the multivalued networkaddress field
    Dim addrBytes() As System.SByte
    For Each addrBytes In serverNetAddr.ByteValueArray

        ' get the first character in the line which indicates type
        Dim type As Char = ChrW(addrBytes(0))

        If type = "9"c Then
            ' only interested in TCP address
            Dim serverTCPAddr As String = ""
            Dim i As Integer
            For i = (addrBytes.Length - 4) To addrBytes.Length - 1
                ' last four bytes are the ip address
                '  Dim b As Byte = CType(addrBytes(i), Byte)  ' convert sbyte to byte



                Dim b As Byte = unchecked(CByte(addrBytes(i)))

                ' Dim b As SByte = unchecked(CType(addrBytes(i), Byte))  ' convert sbyte to byte
                serverTCPAddr = serverTCPAddr + b ' append value to string
                If i > 0 And i < (addrBytes.Length - 1) Then
                    serverTCPAddr = serverTCPAddr + "." ' brute force the dots
                End If
                TextBox6.Text = serverTCPAddr
            Next

        End If

    Next

End If

下一个

我试过“Dim b As Byte = CByte(addrBytes(i))”和“Dim b As SByte = unchecked(CType(addrBytes(i), Byte))”'convert sbyte to byte 无济于事,它一直在 Dim b As Byte 部分崩溃。

谁能告诉我为什么它可以在 C# 而不是 VB.net 中工作,以及可能的修复方法?

谢谢你, 问候, 扎恩·彼得斯

【问题讨论】:

  • 我不认为unchecked 在 VB.NET 中像在 C# 中那样工作 - 您可能会抑制 C# 版本中的溢出错误。您可以在 VB.NET 版本中使用 TRY/CATCH 来捕获溢出
  • VB.NET 默认总是检查溢出。当 sbyte 小于 0 时会报错。不能像在 C# 中那样关闭单个语句,它是一个编译选项。项目 > 属性 > 编译选项卡 > 高级按钮 > 删除整数溢出检查复选框。或者将代码保存在 C# 库中,利用 .NET 中出色的语言互操作性绝不是一个坏主意。
  • 一个名为 Viorel_ 的用户在这里解决了问题:social.msdn.microsoft.com/Forums/vstudio/en-US/… Dim sb As SByte Dim b As Byte sb = -2 b = CByte(sb And &HFF) ' 结果:254 谢谢大家!

标签: vb.net


【解决方案1】:

一个名叫 Viorel_ 的用户在这里解决了这个问题:https://social.msdn.microsoft.com/Forums/vstudio/en-US/5e68f6c5-0e60-4c1f-bcd4-c7aad97e83fb/vbnet-sbyte-to-byte-poroblem?forum=vbgeneral

Dim sb As SByte

Dim b As Byte

sb = -2

b = CByte(sb And &HFF) ' result: 254

谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2014-07-20
    • 2011-10-02
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多