Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:00:27+00:00 2026-06-03T09:00:27+00:00

If I were reading byte numbers I would do: using (FileStream stream = new

  • 0

If I were reading byte numbers I would do:

using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
    int size = (int) stream.Length;
    BinaryReader br = new BinaryReader(stream);
    byte[] test = new byte[size];
    test = br.ReadBytes(size);
    br.Close();
}

But since I want to read Uint16, I am struck doing:

using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
    int size = (int) stream.Length;
    BinaryReader br = new BinaryReader(stream);
    float[] test = new byte[size/2];
    for (int i = 0; i < size/2; ++i)
    {
         test[i] = br.ReadUInt16();
    }
    br.Close();
}

Is there a faster way to read the whole file at once or is the speed difference negligeable ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:00:28+00:00Added an answer on June 3, 2026 at 9:00 am

    If the file isn’t too large to fit in memory, it would be faster to read all the bytes into a MemoryStream and then read Uint16s from that stream. Like so:

    byte[] data;
    int size;
    using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
    {
        size = (int) stream.Length;
        data = new byte[size];
        stream.Read(data, 0, size);
    }
    
    uint[] uintData = new uint[size / 2];
    using (var memoryStream = new MemoryStream(data))
    using (var reader = new BinaryReader(memoryStream))
        for (int i = 0; i < size / 2; i++)
            uintData[i] = reader.ReadUInt16();
    
    // Done.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Typical approaches recommend reading the binary via FileStream and comparing it byte-by-byte. Would a
I'm reading from a byte stream that contains a series of variable length descriptors
When using the Stream.BeginRead Method, and you are reading from a stream into a
I'm trying to split a byte stream into chunks of increasing size. The source
I've read in manual: For bit-oriented delivery, the bit order for the byte stream
I am reading a file byte-by-byte. Say for example i have this byte: 0x41
I am reading a raw audio file (CD track rip) in, doing byte swapping,
I've written a Ruby script that is reading a file ( File.read() ) that
Thank you in advance! I'm reading a BMP in C++ using self-defined BITMAPFILEHEADER and
I'm reading a file into a byte array in chunks and sending it over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.