BinaryFormatter with MemoryStream Question
Asked Answered
O

5

7

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?

Outdoors answered 1/6, 2010 at 20:44 Comment(0)
M
6

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();
        }
Moorehead answered 1/6, 2010 at 20:53 Comment(1)
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).Ringtail
F
2

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.

Falstaffian answered 1/6, 2010 at 20:47 Comment(0)
R
2

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

Ringtail answered 1/6, 2010 at 20:47 Comment(0)
C
2

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.

Casserole answered 1/6, 2010 at 20:48 Comment(0)
P
2

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");
Pfister answered 1/6, 2010 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.