Questions tagged [binaryreader]

In .net is a class used to read binary values. In general, it is a class or a program to read and manipulate binary data.

244 questions
859
votes
4 answers

How to convert byte array to string

I created a byte array with two strings. How do I convert a byte array to string? var binWriter = new BinaryWriter(new MemoryStream()); binWriter.Write("value1"); binWriter.Write("value2"); binWriter.Seek(0, SeekOrigin.Begin); byte[] result =…
Oksana
  • 13,442
  • 10
  • 53
  • 89
77
votes
6 answers

An elegant way to consume (all bytes of a) BinaryReader?

Is there an elegant to emulate the StreamReader.ReadToEnd method with BinaryReader? Perhaps to put all the bytes into a byte array? I do this: read1.ReadBytes((int)read1.BaseStream.Length); ...but there must be a better way.
SamFisher83
  • 3,937
  • 9
  • 39
  • 52
60
votes
5 answers

C# checking for binary reader end of file

I was searching for a way to check whether I've reached the end of a file for my binary reader and one suggestion was to use PeekChar as such while (inFile.PeekChar() > 0) { ... } However, it looks like I've run into an issue Unhandled…
MxLDevs
  • 19,048
  • 36
  • 123
  • 194
47
votes
1 answer

StreamReader vs BinaryReader?

Both StreamReader and BinaryReader can be used to get data from binary file ( for example ) BinaryReader : using (FileStream fs = File.Open(@"c:\1.bin",FileMode.Open)) { byte[] data = new…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
39
votes
7 answers

C# - Binary reader in Big Endian?

I'm trying to improve my understanding of the STFS file format by using a program to read all the different bits of information. Using a website with a reference of which offsets contain what information, I wrote some code that has a binary reader…
mowwwalker
  • 16,634
  • 25
  • 104
  • 157
28
votes
5 answers

Faster (unsafe) BinaryReader in .NET

I came across a situation where I have a pretty big file that I need to read binary data from. Consequently, I realized that the default BinaryReader implementation in .NET is pretty slow. Upon looking at it with .NET Reflector I came across…
andreialecu
  • 3,639
  • 3
  • 28
  • 36
15
votes
4 answers

Downloading pdf file using WebRequests

I'm trying to download a number of pdf files automagically given a list of urls. Here's the code I have: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; var encoding = new…
Aabela
  • 1,408
  • 5
  • 19
  • 28
12
votes
5 answers

EndOfStream for BinaryReader

BinaryReader does not have EndOfStream property. Is it safe to use following code to check if end of stream is reached? reader.BaseStream.Length>reader.BaseStream.Position
melmi
  • 988
  • 3
  • 9
  • 27
11
votes
1 answer

Decompress byte array to string via BinaryReader yields empty string

I am trying to decompress a byte array and get it into a string using a binary reader. When the following code executes, the inStream position changes from 0 to the length of the array, but str is always an empty string. BinaryReader br =…
jkh
  • 3,618
  • 8
  • 38
  • 66
11
votes
3 answers

Is it safe to use Stream.Seek when a BinaryReader is open?

Because of the under the hood buffering strategy of BinaryReader, it is unclear to me whether is it ok or not to read an offset stored in a stream, then reposition the stream at this offset to resume the streaming. As an example, is the following…
Matthieu Durut
  • 4,838
  • 3
  • 20
  • 16
11
votes
3 answers

Efficient way to read big endian data in C#

I use the following code to read BigEndian information using BinaryReader but I'm not sure if it is the efficient way of doing it. Is there any better solution? Here is my code: // some code to initialize the stream value // set the length value to…
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
11
votes
2 answers

What does BinaryReader do if the bytes I am reading aren't present yet?

I am using a BinaryReader on top of a NetworkStream to read data off of a network. This has worked really well for me, but I want to understand what's going on behind the scenes, so I took a look at the documentation for BinaryReader and found it to…
Darkhydro
  • 1,992
  • 4
  • 24
  • 43
10
votes
2 answers

Get Encoding of BinaryReader/Writer?

The .NET BinaryReader/BinaryWriter classes can be constructed with specifying an Encoding to use for String-related operations. I was implementing custom string formats with extension methods, but would yet implement them in a way they respect the…
Ray
  • 7,940
  • 7
  • 58
  • 90
9
votes
1 answer

Empty array with BinaryReader on UploadedFile in c#

Assume the following code: Stream file = files[0].InputStream; var FileLen = files[0].ContentLength; var b = new BinaryReader(file); var bytes = b.ReadBytes(FileLen); If I upload a CSV file that is 10 records ( 257 bytes ), the BinaryReader fills…
Paul Shriner
  • 538
  • 3
  • 9
9
votes
3 answers

Using BinaryWriter or BinaryReader in async code

I have a list of float to write to a file. The code below does the thing but it is synchronous. List samples = GetSamples(); using (FileStream stream = File.OpenWrite("somefile.bin")) using (BinaryWriter binaryWriter = new…
Yusuf Tarık Günaydın
  • 3,016
  • 2
  • 27
  • 41
1
2 3
16 17