views:

133

answers:

1

I have an app that converts binary file into ASCII file. With profiler I found that I spend 25% of time doing Encoding.GetBytes() which is called from BinaryWriter.Write(wchar[]). It is completely correct since I have many constructs similar to this one:

m_writer.Write("some fancy long text".ToCharArray());

Do you have any smart idea how to avoid this encoding conversion?

I now that one idea would be to to do something similar to this:

const byte[] SOME_FANCY_LONG_TEXT = Encoding.ASCII.GetBytes("some fancy ...");
// ... and later
m_writer.Write(SOME_FANCY_LONG_TEXT);

but I have to many such entries to do it manually.

+3  A: 

If you're creating a text file, why are you using BinaryWriter at all? Just use a TextWriter. BinaryWriter is meant for binary streams where you want to write primitives, strings etc in a simple way.

(Is all your text definitely going to be ASCII, by the way? You might want to consider using UTF-8 instead.)

Jon Skeet
Problem is no longer relevant :) Thanks for input - helped greatly.
Michal Sznajder