Converting string from memystream to binary [] contains leading shit

- Edit with more bgnd info -

A (black box) COM object returns me a string. The second COM object expects the same string as byte [] and returns byte [] with the processed data. This will download to the browser as a downloadable, human-readable file to be downloaded in a standalone client side application.

so i get inputString from first COM and convert it to byte [] like this

BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, inputString);
obj = ms.ToArray();

      

I pass it to the second COM and read it back. The result is written to the browser.

Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename="test.dat");
Response.BinaryWrite(obj);

      

The error occurs in the 2nd COm because the formatting is wrong. I went to check the original line and it was fine. Then I downloaded the result of the 1st com directly into the browser and saw what happened. It turned out that somewhere along the road additional unreadable characters were added. What are these characters, what are they used for, and how can I prevent them from making my second COM file stop?

Unreadable characters of this type:

NUL / SOH / NUL / NUL / NUL / FF / FF / FF / FF / SOH / NUL / NUL / NUL etc.

Any ideas?

- answer -
Use

System.Text.Encoding.UTF8.GetBytes(theString)

      

but not

BinaryFormatter.Serialize()

      

+1


source to share


5 answers


Ok, with your updated information: your second COM object is expecting binary data, but you want to create that binary data from a string. Is this treated as simple binary data?

I am guessing that something will change this process on the client side. If you ultimately want to recover the data as a string, you need to choose the correct encoding to use and use it on both sides. UTF-8 is a good bet in most cases, but if the client side is just going to write the data to a file and use it as an XML file, you need to choose the appropriate XML based encoding.



You said the first few characters of the line were just "<foo>"

(or something similar) - doesn't that mean there is no XML declaration? If not, choose UTF-8. Otherwise, you should look at the XML declaration and use that to determine your encoding (again, defaults to UTF-8 if the declaration does not specify an encoding).

Once you've got the correct encoding, use Encoding.GetBytes as mentioned in earlier answers.

+1


source


BinaryFormatter is almost certainly not what you want to use.



If you just need to convert the string to bytes use Encoding.GetBytes for an appropriate encoding of course. UTF-8 is usually correct, but check if the document specifies an encoding.

+2


source


I think you are missing the point of BinarySerialization.

For starters, what type is the Xml formula?

Binary serialization will compress this value in machine notation, not XML! The content will look like this:

    ΓΏΓΏΓΏΓΏ          AIronScheme, Version=1.0.0.0, Culture=neutral, Public

      

You might want to look at an XML serializer.

Update:

You want to output some XML as a "content-disposition" stream.

To do this, do the following:

byte[] buffer = Encoding.Default.GetBytes(formulaXml);
Response.BinaryWrite(buffer);

      

This should work as you hoped (I think).

+1


source


The challenge BinaryFormatter

is to convert objects to an opaque serialization format that can only be understood by others BinaryFormatter

on the other end.

(Just want to mention Encoding.GetBytes

, but John beat me up before.)

You might want to use System.Text.Encoding.UTF8.GetBytes ().

+1


source


Is the crap at the beginning of the two bytes?

It can be a Unicode string byte-encoded character.

http://en.wikipedia.org/wiki/Byte-order_mark

0


source







All Articles