代码之家  ›  专栏  ›  技术社区  ›  sashang

Serializing data types for use over a network in F#

  •  2
  • sashang  · 技术社区  · 14 年前

    我想通过网络发送一个uint16。我查看了不同的.NET序列化程序。根据这个 F# Serialize Discriminated Union why so many bytes? 使用BinaryFormatter将生成表示该类型元数据的开销字节。这样做的结果是,一旦通过格式化程序,uint16就不能表示为16位。我需要一种方法来转换这个uint16,这样我就可以从中得到16位,从而可以发送到套接字另一端的程序。

    2 回复  |  直到 14 年前
        1
  •  3
  •   elmattic    14 年前

    我想你可以用 System.BitConverter 班级:

    static member GetBytes : 
            value:uint16 -> byte[] 
    

    http://msdn.microsoft.com/en-us/library/8wwsdz3k.aspx

        2
  •  0
  •   sashang    14 年前

    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