f u read binary data, what kind of arithmetic operation ur trying to do?
adding 10 to what data?
Main Topics
Browse All TopicsIm developing a Windows application using Visual C# and Visual Studio 2008.
I have a huge binary file that contains millions of items of data of type short. Lets say the file is called "original.dat"
I want to open this file and access the data. I want to do a simple arithmetic operation to the data (lets say add 10) and then store the amended data in a new file called "newfile.dat"
Can someone post the code that lets me do this?
Note the file is so big that loading it in its entirety into memory and then doing the arithmetic operation is not an option.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
First off, all files are technically binary data. Some binary data is just interpreted differently depending on the context. For instance, if I have a text file that contains the word "Test", you won't find the letters 'T', 'e', 's', and 't' on your hard drive anywhere. You'll see a series of binary values corresponding to those letters depending on the encoding you use. Opening the file and treating it as text allows you to view it as text, even if it wasn't. I could open notepad, write a bunch of crap, and save it as a .dat, and it would still have the same binary data as any txt.
If the file is too fat to load all at once, you'll have to load it a chunk at a time (I suggest loading it like 1KB or so at a time, then discarding that data. I forgot how big a sector is nowadays, but that's a different story). Anyways, you could use the BinaryReader and BinaryWriter classes to make your life a tiny bit easier with this (though they're not necessary; they just make "writing binary" (I put that in quotes because, really, what else can you write to a file? Nothing. Just binary, every time).
Anyways, I'm not 100% sure I understand your question. Your "binary file", as you call it, has a bunch of binary in it, I've gathere that much (once again, I ask you, what else could it contain?). You are also saying that the binary within it should be interpreted as a very large series of short integers (ie 16-bit integers). I'm going to assume, for my solution to you, that you wish to add 10 to each short value in this file, and write the 'result' to the other file.
I really do hope this isn't homework, as I don't condone cheating, but here you go. For this example, I'm just going to read one short at a time for simplicity's sake. I suggest that you optimize it yourself (consider this my challenge to you :) ), though you don't have to, of course. Depending on the size of the file, the code I provide should be pretty slow after a certain point (then again, .NET likes to buffer file data without your knowing anyway, so it doesn't make much of a difference, if your file isn't too big...). However, the code should handle any amount of data.
Let me know if you have any questions, but as for optimizing it for you, that's up to you (unless you get it started and I see that you put some geniune effort into it. I may help you out in that case).
Hope it helps,
Nate
I had to laugh when your first comment was "not sure what the problem is".
Anyway thanks for the link. The link you gave shows an example for date of type byte. ie. the input.Read() function requires a byte array. My data is of type short. Do I simply use a factor of 2 for read (and write) to accoutn for the fact 2 bytes is equivalent to 1 short?
Thanks Static I'll peruse your reply and get back to you.
But Im left wondering what this site is all about. I aks a question and Im supposed to get an answer and award points right?.
SlightWv starts off by saying "not sure what the problem is" as though he's a bit annoyed. Thats funny enough but then you start wondering whether Im cheating on my homework and then set me a few challenges.
Anyway keep up the comedy guys.
>> The commentary was addressing what I thought was a pretty simple question that could have been found with a simple google search.
Ehh, yes and no. Binary data scares most people, as most don't really understand it, so I guess I can't blame him. I've certainly seen much, much worse questions, oh lord... Hell, I could pick out like 40 within the first 60 questions. Anyways, most would be surprised to hear that fetching a Byte (8 bits) from RAM actually entails telling the RAM to give you 32 bits of information, padding on the extra 24 bits for you. Binary knowledge isn't common, but I suppose it isn't rare, either. .NET is one of the "languages" in which it is forgiveable to know little about deep details.
As for converting bytes to other data types, rangers99, it's extremely easy in .NET. There are a few ways to do it; however, the easiest one is to use the static class called "BitConverter" in the System namespace. For example, to convert bytes to an Int16 (aka short) using BitConverter, you call the BitConverter.ToInt16 method. You could grab a hunk of your data (an even number, probably something like 1024 bytes or some other power of 2) as a Byte array and pass it to the BitConverter.ToInt16 method with the necessary offset.
As well as using this class, you could just shift left and OR the two bytes you want to "merge" (ie ((Int16)(leftByte << 8)) | ((Int16)rightByte). I can't remember if .NET upcasts operands of bitwise operators to Int32 by default, but I'm too lazy to check, and casting them to Int16 shows your intent anyway (even if it does look a little strange).
I surely hope you're not cheating on your homework, as the number of borderline retarded programmers on the market is already astonishing. I don't know how much more if it I can take, so please, learn your profession, and learn it well. Cheating never helps ;)
Hope it helps,
Nate
Heh, yeah, about the awful "questions" on EE... Check this beauty out.
Moderators: Sorry, I had to do it.
Business Accounts
Answer for Membership
by: slightwvPosted on 2009-11-04 at 06:26:36ID: 25739537
Not sure what the problem is. Just read the file in chunks.
dotnet-cod e/2004/11/ binary-fil e-io- using -methods-o f.html
http://www.dotnet4all.com/