views:

327

answers:

5

I am testing BinaryFormatter to see how it will work for me and I have a simple question:

When using it with the string HELLO, and I convert the MemoryStream to an array, it gives me 29 dimensions, with five of them being the actual data towards the end of the dimensions:

            BinaryFormatter bf = new BinaryFormatter();

            MemoryStream ms = new MemoryStream();

            byte[] bytes;
            string originalData = "HELLO";

            bf.Serialize(ms, originalData);
            ms.Seek(0, 0);

            bytes = ms.ToArray();

returns

-       bytes   {Dimensions:[29]}   byte[]
        [0] 0   byte
        [1] 1   byte
        [2] 0   byte
        [3] 0   byte
        [4] 0   byte
        [5] 255 byte
        [6] 255 byte
        [7] 255 byte
        [8] 255 byte
        [9] 1   byte
        [10]    0   byte
        [11]    0   byte
        [12]    0   byte
        [13]    0   byte
        [14]    0   byte
        [15]    0   byte
        [16]    0   byte
        [17]    6   byte
        [18]    1   byte
        [19]    0   byte
        [20]    0   byte
        [21]    0   byte
        [22]    5   byte
        [23]    72  byte
        [24]    69  byte
        [25]    76  byte
        [26]    76  byte
        [27]    79  byte
        [28]    11  byte

Is there a way to only return the data encoded as bytes without all the extraneous information?

+2  A: 

For a simple string, use a BinaryWriter. The overhead will be reduced to a small length prefix.

BinaryFormatter is intended for serializing (complex) object clusters and needs some auxiliary data structures to do that.

Henk Holterman
+2  A: 

It depends what you actually want. You can get a UTF8 byte array from a string with Encoding.UTF8.GetBytes.

Daniel Earwicker
+2  A: 

You shouldn't strip away all that "extraneous" information. The deserializer needs it on the other end when you want to reconstitute the object from the serialized data.

dthorpe
+2  A: 

Are you just trying to convert the string to a byte array? If that is your goal, you can do something more like:

byte[] bits = System.Text.Encoding.UTF8.GetBytes("HELLO");
yodaj007
+1  A: 

All of that extraneous information tells the other BinaryFormatter (that will deserialize the object) what type of object is being deserialized (in this case, System.String). Depending on the type, it includes other information needed to reconstruct the object (for instance, if it were a StringBuilder, the Capacity would also be encoded in there.

If all you want to do is stuff a string into a MemoryStream buffer:

        using (MemoryStream ms = new MemoryStream())
        using (TextWriter writer = new StreamWriter(ms))
        {
            writer.Write("HELLO");
            writer.Flush();

            byte[] bytes = ms.ToArray();
        }
Toby
Note: the `StreamWriter` constructor internally creates an instance of `UTF8Encoding` to do the actual conversion to bytes. If all you need is an array of bytes corresponding to a string, then `MemoryStream` and `TextWriter` are not actually the solution - they're just a coincidental way of implicitly creating the class that provides the solution. Just use `Encoding.UTF8.GetBytes` directly - or use the correct encoding class for the character encoding you require (UTF-8 is a good default choice).
Daniel Earwicker