Can C# string contain binary data?

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
 
B

Barry Kelly

Edvin said:
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();


Writing a string to a binary writer uses the encoding associated with
the binary writer. First it writes the byte length as a 7-bit encoded
number (so that short strings don't take up too many bytes), and then
gets the bytes for the string according to the encoding associated with
the BinaryWriter. The encoding is specified in the constructor of
BinaryWriter, but it defaults to UTF8 if you don't specify it.

The differences in your case with w2:

1) A different encoding is used, iso-8859-1.
2) The byte array is written out, but the length of the byte array
isn't.

-- Barry
 
M

Mehdi

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:

That's because most caracter encodings do not map every possible byte
values to a displayable caracter. Have a look at the ASCII encoding table
for example and you'll see that many byte values do not map to any caracter
at all or map to non-displayable caracters such as \0, TAB or BEEP. So you
need to use a caracter encoding that can map every possible byte value to a
displayable caracter. Fortuanately, there is one and it's called Base 64.
Have a look at Convert.ToBase64String().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top