VB.net TCP流式传输字节,算术运算导致溢出
我已经在这方面工作了几天了,我只是在转动我的轮子。这是一段vb.net代码,用于连接mikrotik路由器的api。这是在他们的维基上提供的,但是出现了一个错误,我很难找到答案VB.net TCP流式传输字节,算术运算导致溢出,vb.net,byte,bitconverter,mikrotik,Vb.net,Byte,Bitconverter,Mikrotik,我已经在这方面工作了几天了,我只是在转动我的轮子。这是一段vb.net代码,用于连接mikrotik路由器的api。这是在他们的维基上提供的,但是出现了一个错误,我很难找到答案 Public Function Read() As List(Of String) Dim output As New List(Of String) Dim o = "" Dim tmp(4) As Byte Dim count As Long
Public Function Read() As List(Of String)
Dim output As New List(Of String)
Dim o = ""
Dim tmp(4) As Byte
Dim count As Long
While True
tmp(3) = tcpStream.ReadByte()
Select Case tmp(3)
Case 0
output.Add(o)
If o.Substring(0, 5) = "!done" Then
Exit While
Else
o = ""
Continue While
End If
Case Is < &H80
count = tmp(3)
Case Is < &HC0
count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(3), 0, 0}, 0) ^ &H8000
Case Is < &HE0
tmp(2) = tcpStream.ReadByte()
count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(2), tmp(3), 0}, 0) ^ &HC00000
Case Is < &HF0
tmp(2) = tcpStream.ReadByte()
tmp(1) = tcpStream.ReadByte()
count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(1), tmp(2), tmp(3)}, 0) ^ &HE0000000
Case &HF0
tmp(3) = tcpStream.ReadByte()
tmp(2) = tcpStream.ReadByte()
tmp(1) = tcpStream.ReadByte()
tmp(0) = tcpStream.ReadByte()
count = BitConverter.ToInt32(tmp, 0)
Case Else
Exit While 'err
End Select
For i = 0 To count - 1
o += ChrW(tcpStream.ReadByte())
Next
End While
Return output
End Function
我得到“算术运算导致溢出”
我知道这一切是如何运作的,但我只是没有完全理解这里发生了什么。
tcpStream.ReadByte()正在返回
tmp(3)返回128
BitConverter.ToInt32(newbyte(){tcpStream.ReadByte(),tmp(3),0,0},0)^&H8000返回“无限”,我认为这就是问题所在
如果您对其使用的完整代码感兴趣,请访问:
如果没有人能告诉我出了什么问题,我真的很想知道这段代码在做什么。。。希望他们能对他们的代码=P进行注释
非常感谢大家我觉得代码有点奇怪,因为“^”-运算符在c#中表示按位异或,而在vb.net中应使用“xor”。此外,在vb.net中“^”是指数函数。也许它会产生一个溢出,因此你有时会产生无限的溢出。
count = BitConverter.ToInt32(New Byte() {tcpStream.ReadByte(), tmp(3), 0, 0}, 0) ^ &H8000