473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

binary write problem.

PCH
I'm trying to pull a data file that has an image (jpeg) embedded in it.

I can locate the bytes for the image, but when I try to write it to a file,
the image ends up being corrupt!

the kb/b size is the same as the original image, but the data itself isnt.

I have the binary data in a byte array and loop through it to write it out.

When i look at the data i can see the characters are different. Like the
conversion to char isnt the same as the original, etc.

Any ideas?

heres the code...
for (int i = 0; i < bytes.Length; i++)

{

//w.Write(bytes[i]);

w.Write(System.Convert.ToChar(bytes[i]));

}

w.Close();

fs.Close();


Nov 15 '05 #1
7 3846

"PCH" <pc***@hotmail.com> wrote in message
news:ue**************@TK2MSFTNGP09.phx.gbl...
I'm trying to pull a data file that has an image (jpeg) embedded in it.

I can locate the bytes for the image, but when I try to write it to a file, the image ends up being corrupt!

the kb/b size is the same as the original image, but the data itself isnt.

I have the binary data in a byte array and loop through it to write it out.
When i look at the data i can see the characters are different. Like the
conversion to char isnt the same as the original, etc.

Any ideas?

heres the code...
for (int i = 0; i < bytes.Length; i++)

{

//w.Write(bytes[i]);

w.Write(System.Convert.ToChar(bytes[i]));

}

w.Close();

fs.Close();

What is w, a TextWriter?

Why on earth are you converting each byte to a char? A char is a 2-byte
unicode character.

Just write the byte array to a new IO.FileSteam object.

David
Nov 15 '05 #2
PCH
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:ux**************@TK2MSFTNGP12.phx.gbl...

"PCH" <pc***@hotmail.com> wrote in message
news:ue**************@TK2MSFTNGP09.phx.gbl...
I'm trying to pull a data file that has an image (jpeg) embedded in it.

I can locate the bytes for the image, but when I try to write it to a

file,
the image ends up being corrupt!

the kb/b size is the same as the original image, but the data itself isnt.
I have the binary data in a byte array and loop through it to write it

out.

When i look at the data i can see the characters are different. Like the conversion to char isnt the same as the original, etc.

Any ideas?

heres the code...
for (int i = 0; i < bytes.Length; i++)

{

//w.Write(bytes[i]);

w.Write(System.Convert.ToChar(bytes[i]));

}

w.Close();

fs.Close();

What is w, a TextWriter?

Why on earth are you converting each byte to a char? A char is a 2-byte
unicode character.

Just write the byte array to a new IO.FileSteam object.

David

Nov 15 '05 #3
PCH <pc***@hotmail.com> wrote:
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);


If you're just writing bytes, don't bother with a BinaryWriter at all -
just use Stream.Write(byte[], int, int).

The problem with "conversions to char" is that character data and
binary data are fundamentally different things, and shouldn't be mixed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
PCH
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").R eplace(" ","_") + ".jpg");

sw.Write (bytes[i]);

sw.close();

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
PCH <pc***@hotmail.com> wrote:
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);


If you're just writing bytes, don't bother with a BinaryWriter at all -
just use Stream.Write(byte[], int, int).

The problem with "conversions to char" is that character data and
binary data are fundamentally different things, and shouldn't be mixed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
As far as I know StreamWriter is meant for text. I think a FileStream is
what you are looking for.

"PCH" <pc***@hotmail.com> wrote in message
news:uw**************@tk2msftngp13.phx.gbl...
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").R eplace(" ","_") + ".jpg");
sw.Write (bytes[i]);

sw.close();

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
PCH <pc***@hotmail.com> wrote:
Why on Earth? Because I have no idea what to do hehe.
This is how I've setup the writers.
FileStream fs = new FileStream("c:\\test4.jpg", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(fs);


If you're just writing bytes, don't bother with a BinaryWriter at all -
just use Stream.Write(byte[], int, int).

The problem with "conversions to char" is that character data and
binary data are fundamentally different things, and shouldn't be mixed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #6
PCH <pc***@hotmail.com> wrote:
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").R eplace(" ","_") + ".jpg");
No, StreamWriter is *not* what you want - again, that's talking about
characters.
sw.Write (bytes[i]);


That would have written a single byte - again, not what you want.

Just create a FileStream, and call Write on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
PCH
I think i got it!

fs.Write((byte[]) bytes,ipos,bytes.Length -ipos);

image is saved correctly.

Thanks for the help!

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
PCH <pc***@hotmail.com> wrote:
hmm it didnt like this... i looked at the image and its just #s:

StreamWriter sw = new StreamWriter("c:\\test" +
DateTime.Now.ToLongTimeString().Replace(":","_").R eplace(" ","_") +
".jpg");
No, StreamWriter is *not* what you want - again, that's talking about
characters.
sw.Write (bytes[i]);


That would have written a single byte - again, not what you want.

Just create a FileStream, and call Write on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
9
by: gamehack | last post by:
Hi all, I've been wondering when I write a structure like: struct { int a; unsigned int b; float c; } mystruct;
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
4
by: Pedro Leite | last post by:
Good Afternoon. the code below is properly retreiving binary data from a database and saving it. but instead of saving at client machine is saving at the server machine. what is wrong with my...
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
15
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.