G
Guest
Why is binary array written to a file different
than when converting the binary array to string
and then writing it to file! For example:
// --- Allocate byte array
byte [] arrByte = new byte[255];
for( int i=1; i< arrByte.Length; i++ )
arrByte = (byte)i;
// --- Create two file handlers
string file = "BinaryFile";
FileStream fs1 = new FileStream( file + "1", FileMode.Create );
FileStream fs2 = new FileStream( file + "2", FileMode.Create );
BinaryWriter w1 = new BinaryWriter( fs1 );
BinaryWriter w2 = new BinaryWriter( fs2 );
// --- Econde binary to string
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("iso-8859-1");
string str = enc.GetString( this.arrByte );
// --- Write data to file
w1.Write( str ); // why is w1 file != w2 file?
w2.Write( arrByte );
w2.Close();
w2.Close();
Needless to say, I've attempted using several different encodings when
converting binary array to string.
The above example demonstrates the issue. I can't use encoding/decoding or
[Serializable], because I have binary data that needs to be returned as a
string to classic asp application, which uses .NET component responsible for
data retrival.
If it helps, here is what I am intending to do...
Classic ASP calls a COM object (written in C#) which does some socket
communication. In my case, contents of a PDF files are obtained. Because
classic ASP does not recognize Byte datatype (array obtained from socket), I
need to convert it to string where the ASP page will use
Response.BinaryWrite( myBinString ).
In my test files I found that I am unable to convert a binary array to
string successfully. In the past, we used vb6 that used String to store the
result from the Socket Communication. Thus, I believe I should be able to
store binary data in string within the c# language! Which the string will
ultimately be returned to the classic ASP.
The above sample code demonstrates the content of a string written to file
is not same as a binary array! Why is this so, and what am I missing?
Thanks in advance
than when converting the binary array to string
and then writing it to file! For example:
// --- Allocate byte array
byte [] arrByte = new byte[255];
for( int i=1; i< arrByte.Length; i++ )
arrByte = (byte)i;
// --- Create two file handlers
string file = "BinaryFile";
FileStream fs1 = new FileStream( file + "1", FileMode.Create );
FileStream fs2 = new FileStream( file + "2", FileMode.Create );
BinaryWriter w1 = new BinaryWriter( fs1 );
BinaryWriter w2 = new BinaryWriter( fs2 );
// --- Econde binary to string
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("iso-8859-1");
string str = enc.GetString( this.arrByte );
// --- Write data to file
w1.Write( str ); // why is w1 file != w2 file?
w2.Write( arrByte );
w2.Close();
w2.Close();
Needless to say, I've attempted using several different encodings when
converting binary array to string.
The above example demonstrates the issue. I can't use encoding/decoding or
[Serializable], because I have binary data that needs to be returned as a
string to classic asp application, which uses .NET component responsible for
data retrival.
If it helps, here is what I am intending to do...
Classic ASP calls a COM object (written in C#) which does some socket
communication. In my case, contents of a PDF files are obtained. Because
classic ASP does not recognize Byte datatype (array obtained from socket), I
need to convert it to string where the ASP page will use
Response.BinaryWrite( myBinString ).
In my test files I found that I am unable to convert a binary array to
string successfully. In the past, we used vb6 that used String to store the
result from the Socket Communication. Thus, I believe I should be able to
store binary data in string within the c# language! Which the string will
ultimately be returned to the classic ASP.
The above sample code demonstrates the content of a string written to file
is not same as a binary array! Why is this so, and what am I missing?
Thanks in advance