Read UShort and UInteger from filestream

Pirahnaplant

Well-known member
Joined
Mar 29, 2009
Messages
75
Programming Experience
3-5
Here is some code I am trying to use to read a UShort and UInteger from a filestream:

VB.NET:
    Private Function ReadUShort(ByVal fs As IO.FileStream) As UShort
        Dim temp(1) As Byte
        fs.Read(temp, 0, 2)
        Dim v As UShort = temp(0)
        v += temp(1) * 256
        Return v
    End Function

    Private Function ReadUInteger(ByVal fs As IO.FileStream) As UInteger
        Dim temp(3) As Byte
        fs.Read(temp, 0, 4)
        Dim v As UInteger = temp(0)
        v += temp(1) * 256
        v += temp(2) * 65536
        v += temp(3) * 1677216
        Return v
    End Function

The problem is that it doesn't return the right value. Can anyone provide me with a function that works?

Edit: Nevermind, I figured it out, that code works fine.
 
Last edited:
Why not use BinaryReader class and ReadUInt16/ReadUInt32 methods?

Regarding the functions where you have declared FileStream input parameter, bear in mind that most streams has no differences when it comes to reading/writing, the difference is usually only in the provider and if the stream is seekable/readable/writeable etc. So if you declare the parameters as Stream instead such functions greatly increase their value, because now they can also operate on any other type of stream.
 
Back
Top