Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have a byte array of 678560 size.

i want to read data from 0th index to 10478.

is anyway to read data from specified position to length of data.

i wrote code for that but i tried in Google but didn't get perfect answer.

using (StreamReader r = new StreamReader(path))
   string Readalltextofdata = r.ReadToEnd();
   byte[] readalltextofdataintobytes = StringToByteArray(Readalltextofdata);


     public static byte[] StringToByteArray(String hex)
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int j = 0; j < NumberChars; j += 2)
                bytes[j / 2] = Convert.ToByte(hex.Substring(j, 2), 16);
            return bytes;

so in readalltextofdataintobytes created and size is 678560. now from this byte array i want to read from 0th position to 10478.

thanks in advance.
Updated 19-Nov-13 2:47am
CPallini 19-Nov-13 8:22am    
If you need only the first 10479 bytes then why don't you read just them from the file?
bunty swapnil 19-Nov-13 8:28am    
i file i want to read in two parts.
and in my scenario file is already opened.i can not open it again. so i read only through stream reader. and then using readtoend store bi file in a string. after that through string to byte array make a pair of 2 char and convert it into bytes.

so i have byte array readalltextofdataintobytes. and if i will change my code i have to change more functionality.
even i use this code also.


but giving eror value can not be null.

Use Linq
var someBytes = readalltextofdataintobytes.Take(10478).ToArray();

You can also use Skip to skip ahead past the bytes you don't want to read.

Hope this helps,
Share this answer
Actually the most efficient way to do this, aside from pinning and unsafe code, is to use the Buffer.BlockCopy[^] method.

In your case, it would be:

byte[] source = //...
byte[] byteSection = new byte[10478];
Buffer.BlockCopy(source, 0, byteSection, 0, byteSection.Length);

That way to copy more/less data just change the size of the byteSection array.
Share this answer
bunty swapnil 19-Nov-13 10:19am    
i wrote Buffer.BlockCopy(source, 66680, byteSection, 0, byteSection.Length)
its not woking. byte section. this size and index will change.

for example i have byte array of 10 and i destination array i want to read from 4th to 9th.

can you clear the above code.
Ron Beyer 19-Nov-13 10:25am    
The second parameter is the starting point in the source array to begin taking data out, are you really wanting to start at the 66680 byte?

If you have a byte array of 10, and you want 4 to 9, here is what you would do:

byte[] source = new byte[10];
byte[] destination = new byte[6];
Buffer.BlockCopy(source, 4, destination, 0, destination.Length);

Which will copy 6 bytes (4 through 9) from source and put them in destination.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900