开发者

Understanding the code..!

You cannot vote on your own post 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, te开发者_JAVA技巧mp 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

If somebody help me understand the following pat of code i'd be obliged..

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


It puts a value in the array using a variation of varint encoding.

Using the varint encoding, a value is stored seven bits at a time starting with the least significant bits, and using the eighth bit as stop bit when the remaining value is zero. This is the method that the BinaryWriter.Write7BitEncodedInt method is using.

The code above only handles a value using no more than 14 bits, and stores the most significant bits first, using the eighth bit as a continue flag instead of a stop flag.

I.e. a value less than 128 is stored in a single byte, while a value that is 128 or greater is stored with the most significant seven bits first, with the eighth bit set to flag that it's a two-byte value, then the other seven bits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜