tags:

views:

52

answers:

3

I am trying to read data from a file stream as shown below:

fileStream.Read(byteArray, offset, length);

The problem is that my offset and length are Unsigned Ints and above function accepts only ints. If I typecast to int, I am getting a negative value for offset which is meaningless and not acceptable by the function.

The offset and length are originally taken from another byte array as shown below:

BitConverter.ToUInt32(length, 0); //length is a 4 byte long byte-array

What is the right way to read from arbitrary locations of a file stream.

A: 

if you are having problems with conversion, something similar might help:

    uint myUInt;
    int i = (int)myUInt; or
    int i = Convert.ToInt32(myUInt);
Asad Butt
+4  A: 

I am not sure if this is the best way to handle it, but you can change the position of the stream and use offset 0. The Position is of type long.

fileStream.Position = (long)length;
fileStream.Read(byteArray, 0, sizeToRead);
Elisha
+1  A: 

For such a filesize you should read your file in small blocks, proccess the block and read the next. int.MaxValue is about ~2GB, uint.MaxValue ~4GB. Such a size doesn't fit in most computers ram ;)

chriszero
Exactly. 640K - erm - 1GB ought to be enough for anybody.
Benjamin Podszun