VB6 can do, C# can NOT do !!! Byte [] into string is invalid data

G

Guest

I can't be the only one who has faced this!

Objective: store byte array into a string.
Require: the string will be returned to classic ASP, which means that the
content can not be encoded, nor can it be serialized.

Greetings all,

From classic ASP I am accessing C# COM object which is responsible for
network socket communications and returning binary data. The classic ASP is
to take the returned binary data and send it to browser via
Response.BinaryWrite( myBinaryVarient ) to display a PDF document.

In past we did this using VB6 COM objects, where the binary data was stored
in String. However, despite my efforts, I am unable to save binary data into
a string without affecting the data when using C#. Here is a sample code
that demonstrates writing a binary array into a file; and converting the
array into string and then writing it to file. You'll find that the two
files are NOT the same!

Do you have any ideas on this matter...
tnx in advance

// --- 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();
 
A

Andrew Faust

Edvin said:
I can't be the only one who has faced this!

Objective: store byte array into a string.
Require: the string will be returned to classic ASP, which means that the
content can not be encoded, nor can it be serialized.

C# can do this just fine, however, you have to do it via an encoding.
Because all strings in C# are Unicode strings, you need to tell the
runtime what character encoding the byte array is in. For example if
it's just a plain Ascii encoding you would use:

System.Text.Encoding ascii = System.Text.Encoding.ASCII;

byte[] byteArray = <something>

string unicodeString = ascii.GetString(byteArray);

Andrew Faust
 
J

Jon Skeet [C# MVP]

Edvin said:
I can't be the only one who has faced this!

Objective: store byte array into a string.
Require: the string will be returned to classic ASP, which means that the
content can not be encoded, nor can it be serialized.

Well that's a problem to start with.

1) *Any* conversion from binary to text is encoding
2) Any "naive" conversion like this is likely to come to grief at some
point. You shouldn't treat arbitrary binary data as text. Use base64
encoding or something smiilar - that's what it's there for. Using
ISO-Latin-1 is your best chance of getting it right with your current
method, but it's still a really bad idea.
From classic ASP I am accessing C# COM object which is responsible for
network socket communications and returning binary data. The classic ASP is
to take the returned binary data and send it to browser via
Response.BinaryWrite( myBinaryVarient ) to display a PDF document.

So far so good. No need for strings yet.
In past we did this using VB6 COM objects, where the binary data was stored
in String.

That doesn't make it a good idea. If it's binary data in one place and
it wants to be binary data later on, why not just keep it as binary
data?
However, despite my efforts, I am unable to save binary data into
a string without affecting the data when using C#. Here is a sample code
that demonstrates writing a binary array into a file; and converting the
array into string and then writing it to file. You'll find that the two
files are NOT the same!

Indeed - because BinaryWriter.Write "Writes a length-prefixed string to
this stream" (to quote the docs).

If you want an identical file, you should use StreamWriter instead of
BinaryWriter.

As I say though, it would be better to either keep it as binary data or
use base64.
 
G

Guest

Jon,
tnx for indepth reply.

Your comment
So far so good. No need for strings yet.
lead me to resolving the issue.

The VB6 code had proven itself for several years during high traffic season.
Which is part of the reason for my attemt to duplicate it.
Also, the I had difficulty returning the binary data back to the caller
(classic ASP). When I tried to examine indexed byte array elements in
vbscript (type cast), I was getting a type mistmatch message, which made me
"assume" there was a problem with data being returned. However,
Response.BinaryWrite method processed the result as it was intended.

Thanks again
Best regards,
Edvin
 

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