Serializing data types for use over a network in F#
I want to send a a Uint16 over the network. I've had a look at the different .NET serializers available. According to this F# Serialize Discriminated Union why so many bytes? using a BinaryFormatter will generate overhead bytes that represent meta-data for that type. A result of this would be that UInt16, once passed through that formatter, may not be represented as 16 bits. I need a way to convert that UInt16 such that I get 16 bits from it that I can send to the program at the other end of the socke开发者_运维技巧t.
I think you can use System.BitConverter
class:
static member GetBytes :
value:uint16 -> byte[]
http://msdn.microsoft.com/en-us/library/8wwsdz3k.aspx
I just found the BitConverter class and it looks like it does what I want:
byte_array = BitConverter.GetBytes header.id
where header.id is UInt16
精彩评论