Hello everyone,
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Here is my code:
/// <summary>
/// Opens a file chooser.
/// </summary>
///
private void openToolStripMe nuItem_Click(ob ject sender,
EventArgs e)
{
openFileDialog. ShowDialog();
string fileName = openFileDialog. FileName;
FileStream fileReader = new FileStream(file Name,
FileMode.Open, FileAccess.Read );
StringBuilder buffer = new StringBuilder() ;
long length = fileReader.Leng th;
long position = 0;
while (position < length) // is this correct ?? or should
it be length - 1 instead of length ?
{
position++;
buffer.Append(f ileReader.ReadB yte());
}
textBox.Text = buffer.ToString ();
}
} 6 14667
Seabass <sh****@upei.ca wrote:
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Yes - read it in blocks instead of reading a byte at a time.
Note that it's also a bad idea to save the results in a StringBuilder.
That's for *strings*, not binary data. MemoryStream is, in some ways,
the binary equivalent of StringBuilder.
I have a class in my miscutil library that helps to read the whole of a
stream in one go. See http://pobox.com/~skeet/csharp/miscutil
Note also that you should use a "using" statement so that your stream
is always closed. (In fact, you're *never* closing it in your code.)
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Hi,
"Seabass" <sh****@upei.ca wrote in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
Hello everyone,
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Well, what do you expect? of course it will takes forever.
A better approach is to read a chunk of the file and then process it from
memory.
Also beware with big files, using StringBuilder may not be the best way for
this.
I would just read what the screen can show, in this way you do not read
content that will not be visible in the screen
Seabass wrote:
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Others have already suggested reading many bytes instead
of one byte at a time.
If you really like ReadByte you can just wrap your Stream
in a BufferedStream, that should get rid of the worst ineffciencies
of using ReadByte.
Arne
On May 13, 1:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
Seabass wrote:
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Others have already suggested reading many bytes instead
of one byte at a time.
If you really like ReadByte you can just wrap your Stream
in a BufferedStream, that should get rid of the worst ineffciencies
of using ReadByte.
Arne
Hi there,
Check the BufferedStream class that adds a buffering layer to read and
write operations of other streams. http://msdn2.microsoft.com/en-us/lib...redstream.aspx
Hope this helps.
Moty
Moty Michaely wrote:
On May 13, 1:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
>Seabass wrote:
>>I'm trying to do a Hex Viewer in C#. I'm reading the user selected file byte by byte but it seems to take forever ( one minute to read a larger file ). Is there any way to make this function work faster ?
Others have already suggested reading many bytes instead of one byte at a time.
If you really like ReadByte you can just wrap your Stream in a BufferedStream, that should get rid of the worst ineffciencies of using ReadByte.
Check the BufferedStream class that adds a buffering layer to read and
write operations of other streams.
That was already in what you quoted ...
Arne
On May 20, 2:05 am, Arne Vajhøj <a...@vajhoej.d kwrote:
Moty Michaely wrote:
On May 13, 1:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
Seabass wrote: I'm trying to do a Hex Viewer in C#. I'm reading the user selected file byte by byte but it seems to take forever ( one minute to read a larger file ). Is there any way to make this function work faster ?
Others have already suggested reading many bytes instead
of one byte at a time.
If you really like ReadByte you can just wrap your Stream
in a BufferedStream, that should get rid of the worst ineffciencies
of using ReadByte.
Check the BufferedStream class that adds a buffering layer to read and
write operations of other streams.
That was already in what you quoted ...
Arne
Yep, Sorry for that, I missed that :)
Moty This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Job Lot |
last post by:
I have tab delimited text file which gets populated on daily basis via
automated process. New entry is written at the bottom. I need to create a
utility which makes a copy of this file with 10 most recent entries.
When I read line using StreamReader object it starts from the top, so
looping though lines and keeping track of the line is not...
|
by: Nick |
last post by:
I have found a class that compresses and uncompresses data but need
some help with how to use part of it below is the deflate method which
compresses the string that I pass in, this works OK. At the end of
this message is the inflate method this is where I get stuck I know
that I need a byte array but because I am decompressing a string I...
|
by: dale zhang |
last post by:
Hi,
I am trying to read an image from MS Access DB based on the following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
The article author is using PictureBox for windows application, while I am
doing for web. I can only find Image from web forms control and HTML control.
This may be the root cause of my problem. For...
|
by: dale zhang |
last post by:
Hi,
I am trying to save and read an image from MS Access DB based on the
following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
Right now, I saved images without any errors. After reading the ole object
from db, I saved it to C: as file1.bmp and displayed on the web. But it can
not be displayed. After I manually sent...
|
by: T Cordon |
last post by:
I am using a StreamReader to read text from an HTML file and display it as
part of a page in a Label Control. Buy it is not displaying characters as:
ñ, ó, ú, etc.
Please Help.
Thanks
| |
by: Mad Scientist Jr |
last post by:
i'm trying to read a file byte by byte (and later alter the data and
write it to a 2nd file byte by byte) and running into a problem where
it seems to keep reading the same byte over and over again (an endless
loop). i thought that BinaryReader.ReadByte advanced to the next byte?
i had it time out after 1000 iterations, and keeps outputting...
|
by: junk5 |
last post by:
Hi
I need to read raw 16 bit data from a file, where the first byte is the
most significant byte of the first data value and the second byte is
the least significant byte of the first data value (the next pair or
bytes and the next etc. specify the second and third values).
For example, if I had a file containing
0xff22
|
by: Freddy Coal |
last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
'--------------------------
Public Shared Function Read_bin(ByVal ruta As String)
Dim cadena As String = ""
Dim dato As Array
If File.Exists(ruta) = True Then
|
by: Jm.GlezdeRueda |
last post by:
Hi all,
Im trying to read a 24bit bmp with fread, and i have some problems.. I
want to read the whole structure in one time, but i dont know why, it
only reads the first member well..
I have two questions..
1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1,
sizeof(struct bmp), 1, fin); i have a Segment violation ??
|
by: Stephen |
last post by:
I am reading a text file using TextReader reader = new
StreamReader("file.txt"); like I have done many a times before. I then
execute the statement string a = reader.ReadToEnd(); this string is then
sent into a function to convert its contents (hex string) into a byte array.
Well when this pass occurs the HextoByteArray function says the input...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |