理解代码..!

时间:2011-06-05 11:50:28

标签: vb.net

您不能在自己的帖子上投票 0

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class SNMP

    Public Sub New()
    End Sub
    Public Function [get](ByVal request As String, ByVal host As String, ByVal community As String, ByVal mibstring As String) As Byte()
        Dim packet As Byte() = New Byte(1023) {}
        Dim mib As Byte() = New Byte(1023) {}
        Dim snmplen As Integer
        Dim comlen As Integer = community.Length
        Dim mibvals As String() = mibstring.Split("."c)
        Dim miblen As Integer = mibvals.Length
        Dim cnt As Integer = 0, temp As Integer, i As Integer
        Dim orgmiblen As Integer = miblen
        Dim pos As Integer = 0
        ' Convert the string MIB into a byte array of integer values
        ' Unfortunately, values over 128 require multiple bytes
        ' which also increases the MIB length
        For i = 0 To orgmiblen - 1
            temp = Convert.ToInt16(mibvals(i))
            If temp > 127 Then
                mib(cnt) = Convert.ToByte(128 + (temp \ 128))
                mib(cnt + 1) = Convert.ToByte(temp - ((temp \ 128) * 128))
                cnt += 2
                miblen += 1
            Else
                mib(cnt) = Convert.ToByte(temp)
                cnt += 1
            End If
        Next
        snmplen = 29 + comlen + miblen - 1

如果有人帮助我理解下面的代码,我有义务......

For i = 0 To orgmiblen - 1
        temp = Convert.ToInt16(mibvals(i))
        If temp > 127 Then
            mib(cnt) = Convert.ToByte(128 + (temp \ 128))
            mib(cnt + 1) = Convert.ToByte(temp - ((temp \ 128) * 128))
            cnt += 2
            miblen += 1
        Else
            mib(cnt) = Convert.ToByte(temp)
            cnt += 1
        End If

1 个答案:

答案 0 :(得分:2)

它使用varint编码的变体在数组中放置一个值。

使用varint编码,从最低有效位开始一次存储7位,并在剩余值为零时将第8位用作停止位。这是BinaryWriter.Write7BitEncodedInt方法正在使用的方法。

上面的代码只处理一个不超过14位的值,并首先存储最高有效位,使用第8位作为继续标志而不是停止标志。

即。小于128的值存储在单个字节中,而128或更大的值存储在最高有效的7位中,第8位设置为标记它是2字节值,然后是其他7位