[2005] Reading bits in Big Endian format.
I made the following function that reads a specified number of bits from my internal buffer and returns the value
vb.net Code:
''' <summary>
''' Reads an unsigned value from the buffer.
''' </summary>
''' <param name="index">The index to start reading.</param>
''' <param name="length">The number of bits to read.</param>
''' <param name="OutputTo">When this method returns, contains the unsigned value read.</param>
''' <returns>An unsigned value read from the buffer.</returns>
''' <exception cref="ArgumentException">This method cannot read more than 64 bits.</exception>
''' <exception cref="ArgumentException">The value to be read extends past the end of the buffer.</exception>
Public Shared Function unsigned(ByVal index As Int32, ByVal length As Int32, Optional ByRef OutputTo As Long = 0) As Long
If length > 64 Then
Throw New ArgumentException("length cannot be greater than 64")
Exit Function
End If
If index + (length * 8) > OdwinBuffer.Length Then
Throw New ArgumentException("end of buffer reached")
Exit Function
End If
Dim num As Integer = 0
Dim total As Integer = 0
Do
If GetBitflagState(index, CInt(Math.Ceiling(length / 8)), num) Then
total = total Or (1 << num)
End If
num += 1
Loop While num < length
OutputTo = total
Return OutputTo
End Function
GetBitflagState is a custom function that determines if a bit is set or not.
I know that big endian just reverses the bytes and it would be easy enough to read it like that but I need to read the bits. I have never worked with big endian before.
any help is great. thanks, troy.