up vote 0 down vote favorite

I have a byte array containing bytes from a file(see my last question) now I want to get the second lot of 4 bytes from the array and convert them to an integer something like bytearray.get(4[start],4[length])

flag

2 Answers

up vote 2 down vote accepted
Dim result as Int32

result = BitConverter.ToInt32(bytearray, 4)
link|flag
up vote 1 down vote
Public Function ByteArrayToInteger(ByRef ByteArray() As Byte, ByRef StartIndex As Integer, ByRef EndIndex As Integer) As Integer
    Dim bSubArray(0 To EndIndex - StartIndex) As Byte
    For i As Integer = StartIndex To EndIndex
        bSubArray(i - StartIndex) = ByteArray(i)
    Next
    Return BitConverter.ToInt32(bSubArray, 0)
End Function

'calling it :

Dim b() As Byte = {1, 2, 3, 4, 5, 6}
Dim x As Integer = ByteArrayToInteger(b, 0, 3)
link|flag
note that to convert bytes to an integer you need at least 4 bytes to do the conversion or an exception will be thrown – Microgen Jan 2 at 14:02
I know, but I think this answer is too complicated the above one seems simple. Is this way a better way to do it? – Jonathan Jan 2 at 14:17
its more flexible with this way – Microgen Jan 8 at 10:47

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.