希望有人可以帮助我。
我有以下代码返回我从这里获得的IP地址:
在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)))
”然后我收到以下错误消息:
&#34; System.OverflowException:算术运算导致了 。溢出&#34;
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))
”&#39; convert sbyte to byte
无济于事,它继续在Dim b As Byte部分崩溃。
任何人都可以告诉我为什么它在C#中运行而不是VB.net,可能还有一种解决方法吗?
谢谢你, 问候, Zain Peters
答案 0 :(得分:0)
名为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
谢谢大家!