I need to read a binary file which has mixed data types embedded in it.
Fixed length strings, 16-bit integers and zero (null) terminated ASCII
strings. (akak ASCIIZ)
The first two types are no problem, but the zero terminated string is a
problem because I have to read byte--by-byte with BinaryStream(), and I
don't know whether the class or the O/S uses buffering to make this
efficient. Is my code slowing the app down - is there a better class or
technique other than reading in byte-by-byte and checking for char 0?
(Code below)
Thanks,
Carlo
/// <summary>
/// Read an AsciiZ string in from the binary reader
/// </summary>
/// <param name="dicReader ">Binary reader instance</param>
/// <returns>String , null terminator is truncted,
/// stream reader positioned at byte after null</returns>
public static string ReadStringZ(Bin aryReader reader) {
string result = "";
char c;
for (int i = 0; i < reader.BaseStre am.Length; i++) {
if ((c = (char) reader.ReadByte ()) == 0) {
break;
}
result += c.ToString();
}
return result;
} 3 5634 Is my code slowing the app down
You tell us! Are you having performance problems that you think are
related to this procedure? If not, I would't worry.
If you want to improve something, I would try replacing the string
concatenation (which creates a new string object for each iteration)
with a StringBuilder.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Thanks for the StringBuilder tip - I will use it, as well as in other cases
where I am concatenating strings. I'm at the beginning of a project to
duplicate the functionility of an extensive database dictionary originally
written in Clipper for DOS, so I am trying to implement best practices early
on.
Does that mean that it's KNOWN that BinaryReader uses buffering that makes
byte-by-byte reads efficient? I don't know much about the internals of
stream readers.
Thanks for the reply,
Carlo
"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:OA******** *******@tk2msft ngp13.phx.gbl.. . Is my code slowing the app down
You tell us! Are you having performance problems that you think are related to this procedure? If not, I would't worry.
If you want to improve something, I would try replacing the string concatenation (which creates a new string object for each iteration) with a StringBuilder.
Mattias
-- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
>Does that mean that it's KNOWN that BinaryReader uses buffering that makes byte-by-byte reads efficient? I don't know much about the internals of stream readers.
I wouldn't assume that, those kind of things are implementation
dependent. But I know that at least on the desktop .NET framework, the
underlying FileStream does buffering.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ravi |
last post by:
Hi,
I have about 200GB of data that I need to go through and extract the
common first part of a line. Something like this.
>>>a = "abcdefghijklmnopqrstuvwxyz"
>>>b = "abcdefghijklmnopBHLHT"
>>>c = extract(a,b)
>>>print c
"abcdefghijklmnop"
|
by: Gordon Airport |
last post by:
Has anyone suggested introducing a mutable string type (yes, of course)
and distinguishing them from standard strings by the quote type - single
or double? As far as I know ' and " are currently interchangeable in all
circumstances (as long as they're paired) so there's no overloading to
muddy the language. Of course there could be some interesting problems
with current code that doesn't make a distinction, but it would be dead
easy to fix...
|
by: Sergey Dorofeev |
last post by:
I can use string.unpack if string in struct uses fixed amount of bytes.
But is there some extension to struct modue, which allows to unpack
zero-terminated string, size of which is unknown?
E.g. such struct: long, long, some bytes (string), zero, short, short,
short.
|
by: scroopy |
last post by:
Hi,
I've always used std::string but I'm having to use a 3rd party library
that returns const char*s. Given:
char* pString1 = "Blah ";
const char* pString2 = "Blah Blah";
How do I append the contents of pString2 to pString? (giving "Blah
Blah Blah")
|
by: Ben |
last post by:
I have an int variable (always <100) that I want to convert to a two character string, e.g.
if myint = 1, mystr = "01"
if myint = 81, mystr = "81"
At the moment I can't figure out how to do this cleanly.
Then I wish to push a bunch of these strings into an array, for example:
| |
by: Potiuper |
last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C?
Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
|
by: cman |
last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like
you can with arrays. If not, why not? If so, how?
cman
|
by: hstagni |
last post by:
Where can I find a library to created text-based windows applications?
Im looking for a library that can make windows and buttons inside
console.. Many old apps were make like this, i guess
____________________________________
| |
| ------------------ |
| | BUTTON | |
| ...
|
by: monkey0525 |
last post by:
I'm writing a program that reads information from three seperate classes. Here is my code:
public class Animal
{
protected int id;
protected String type;
protected double mass;
//------------------------------------------------------------------------
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |