1

Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes.

Found this on google. I use .Net to send packets using a socket connection. I use Encoding.Default.GetBytes().

Serialization is confusing, what is the difference of it to GetBytes?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Malcolm Who
  • 433
  • 5
  • 13

2 Answers2

2

No, GetBytes() just converts a string to its binary represenation using the asked encoding. So it does serialize, but it is very specific to strings.

When you want to serialize any object to bytes, you could use the BinaryFormatter. More about how to serialize an object using the BinaryFormatter can be found here.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • GetBytes() then serialize, is that what should I do? – Malcolm Who Jun 23 '15 at 08:28
  • No, it is serialized already, but it can only serialize a `string`. If you want to serialize another type of object, use `BinaryFormatter`. – Patrick Hofman Jun 23 '15 at 08:29
  • So, GetBytes is basically a serialization of strings? I currently practicing on making a chat application via socket, therefore, GetBytes is enough? – Malcolm Who Jun 23 '15 at 08:31
  • 1
    @MalcolmWho Normally it isn't enough. Depending on the transmission medium, you'll have to write somewhere the length of the encoded string (often you'll prepend it to the string, using `BitConverter.GetBytes(lengthofserializedstring)` to get a `byte[]` and sending it before sending the `byte[]` you received by `Encoding.Default.GetBytes()`). ah... and you should use `Encoding.UTF8.GetBytes` if possible – xanatos Jun 23 '15 at 08:49
  • @MalcolmWho: Yes, if that is the only message going around. May I suggest to take a loot at [SignalR](http://signalr.net/)? It uses web sockets to communicate and it providing in a lot of functionality you need. – Patrick Hofman Jun 23 '15 at 08:49
  • I do not agree at all that GetBytes() could be regarded as "special" serialization! – Martin Meeser Jun 23 '15 at 12:23
  • @MartinMeeser: Want to explain why? From the coder perspective it is. – Patrick Hofman Jun 23 '15 at 12:23
  • @PatrckHofman you really are not aware of the difference? I mean ... from the coder perspective? – Martin Meeser Apr 29 '16 at 19:30
1

In .NET you too have serialization. You have binary serialization, XML serialization and JSON serialization.

With serialization you transform an object to another format in order to store it or to send it over the network. An serialized object can be deserialized to an instance of the object (if the class is available at runtime).

With GetBytes() you get the Byte Representation of a variable at runtime. There is no information about the type. You could interpret the Bytes as any type you want, creating unuseful results of course. GetBytes() returns the raw bytes as it is stored in your physical memory.

Martin Meeser
  • 2,784
  • 2
  • 28
  • 41
  • "With GetBytes() ... There is no information about the type." There is: it is a `string`. – Patrick Hofman Jun 23 '15 at 08:37
  • Yes I see, I have been a little mistaken with BitConverter.GetBytes(), but I think basic idea still holds. – Martin Meeser Jun 23 '15 at 12:19
  • @Patrick: Within the bytes created by GetBytes(), is there any information that it has to be converted to a string? No it does not. When send over the network to another .NET process, has the the other process any change of detecting which type the bytes should be converted to. – Martin Meeser Jun 23 '15 at 12:24
  • If there are just strings exchanged, there is no need to. – Patrick Hofman Jun 23 '15 at 12:25