You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不同类型变量序列化后的字节形式及转换实现问询

Hey there! Let's break down what's going on with your BinaryFormatter serialization, and why even a simple byte value spits out that long byte sequence.

Understanding How BinaryFormatter Serializes Data

First off, BinaryFormatter doesn't just serialize the raw value of your variable—it packs full type metadata alongside it. This metadata lets the deserializer exactly recreate the original object (including knowing what type it was) later on. That's why your 1-byte byte variable turns into a much longer byte array: most of those bytes are describing the type, not just the value.

Breaking Down the Byte Sequence for a byte Value

The sequence you shared (0 1 0 0 0 255 255 255 255 1 0 0 0 0 0 0 0 4 1 0 ...) has three key parts:

  • Header bytes: The first few bytes are a marker that identifies this as a .NET serialization stream.
  • Type metadata: This chunk includes information about the System.Byte type—like its assembly name, type name, and other metadata needed to reconstruct the type during deserialization.
  • Raw value: Buried in that sequence is the actual 0 value you serialized (you’ll spot a single 0 byte later in the array, that’s your variable’s raw data).
Serialization Output for Common C# Types

Here’s what you can expect for other common types when using BinaryFormatter:

  • Primitive value types (int, float, bool): Each will include type metadata followed by the raw byte representation of the value. For example, int a = 10 will serialize to metadata for System.Int32 plus 4 bytes (0x0A 0x00 0x00 0x00, since .NET uses little-endian).
  • Strings: Alongside System.String metadata, you’ll get the string’s length, encoding info, then the actual byte content of the string. For string s = "Hi", that means metadata + length (2) + the bytes 0x48 0x69.
  • Custom structs: The formatter will serialize the struct’s type metadata, followed by metadata and values for each field in the struct. For a struct with an int and byte, you’ll see the struct’s metadata first, then the int’s metadata+value, then the byte’s metadata+value.
  • Null values: The sequence will include a marker indicating null, with no raw value bytes attached.
If You Only Need Raw Value Bytes

If your goal is to serialize just the raw value (no extra metadata), BinaryFormatter is overkill. Instead, use lighter-weight options:

  • For primitive types, use BitConverter: BitConverter.GetBytes((byte)0) returns a single 0 byte, and BitConverter.GetBytes(123) returns the 4-byte int representation.
  • For custom structs (marked with [StructLayout(LayoutKind.Sequential)]), use the Marshal class to copy the struct directly to a byte array.
  • Use BinaryWriter to manually write each value to a MemoryStream—this gives you full control over exactly which bytes are output.

内容的提问来源于stack exchange,提问作者John Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:34:28