.net下二进制序列化的格式分析

作者 :zfive5

email:[email protected]

相应 c#下的序列化代码如下所示,程序把序列化后的数据存入了一个指定的文件file.bin里,分析这个文件数据主要为了能让非.Net下的应用程序读取序列化后数据,这有助于.net与其他语言平台的交互.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace WindowsApplication2

{

[Serializable]

public class Object5

{

public int i1 = 0 ;

public int i2 = 0 ;

public float f3= 0 ;

public string str;

}

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.ComponentModel.Container components = null ;

public Form1()

{

InitializeComponent();

}

protected override void Dispose( bool disposing )

{

if ( disposing )

{

if (components != null )

{

components.Dispose();

}

}

base .Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

private void InitializeComponent()

{

this .button1 = new System.Windows.Forms.Button();

this .SuspendLayout();

this .button1.Location = new System.Drawing.Point( 72 , 72 );

this .button1.Name = "button1" ;

this .button1.Size = new System.Drawing.Size( 128 , 32 );

this .button1.TabIndex = 0 ;

this .button1.Text = "button1" ;

this .button1.Click += new System.EventHandler( this .button1_Click);

this .AutoScaleBaseSize = new System.Drawing.Size( 6 , 14 );

this .ClientSize = new System.Drawing.Size( 292 , 273 );

this .Controls.Add( this .button1);

this .Name = "Form1" ;

this .Text = "Form1" ;

this .ResumeLayout( false );

}

#endregion

[STAThread]

static void Main ()

{

Application.Run( new Form1());

}

private void button1_Click( object sender, System.EventArgs e)

{

Object5 obj = new Object5();

obj.i1 = 128 ;

obj.i2 = 24 ;

obj.f3= 1.3f ;

obj.str = "Some String" ;

double d1= 1.3d ;

float f1= 1.3f ;

int i1= 1 ;

string s1= "HelloWorld" ;

System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

System.IO.Stream stream = new System.IO.FileStream( "File.bin" , System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);

formatter.Serialize(stream, obj);

formatter.Serialize(stream,d1);

formatter.Serialize(stream,f1);

formatter.Serialize(stream,i1);

formatter.Serialize(stream,s1);

stream.Close();

formatter= null ;

}

}

}

文件内容如下图所示 :

注解 :

1 ) 00 01 00 00 00 FF FF FF FF 01 00 00 00 00 00 00 00 序列化头 , 经过实践分析 , 这部分基本在每个序列化后的数据都一样 , 下面也可以看到与其他的一样

2 ) 0C 02 被序列化后对象描述信息的表示符

3 ) 00 00 00 51 ( 81 )长度 ,( 注意是大端的 4 位整型 ), 说明后面对象描述信息的长度

4 )自定义对象的描述信息 , 分析时可以忽略,长度为 81 个字符(前一项以说明)

5 ) 05 01 自定义对象的表示符 .

6 ) 00 00 00 1b (27) 自定义对象类描述信息的长度

7 )自定义对象的描述信息 , 主要类的符号表示 (“ WindowsApplication2.Object5”)

8 )自定义对象中的成员条目,例如在上面定义的对象中有四项,分别为 int 、 int 、 flaot 和 string

9 ) 02 长度为 2 的 int i1 成员

10 )对应成员名称的定义标示 “i1”

11 )同 9 项

12 )同 10 项

13 )同 9 项

14 )同 10 项

15 )同 9 项

16 )同 10 项

17 )说明自定义对象的各个定义项目是值对象还是其他类型,一共 4 个字节, 00 为值对象

01 为字符对象 在上面定义的对象为 int i1 int i2 float f1 string str 对应为 00 00 00 01 。

18 )说明 17 )对应的字段的类型 分别为 int 08 int 08 int 0b

19 ) 02 00 00 00 固定,说明其他数据的开始

20 )对应 obj.i1 的值 80 00 00 00 ( 128 、小端格式)

21 )对应 obj.i2 的值 18 00 00 00 ( 24 、小端格式)

22 )对应 obj.f1 的值 66 66 a6 3f ( 1.3 的浮点格式)

23 ) 06 03 对象 string 标示符,说明它是 string 对象

24 、 25 ) 00 00 00 0b string 对象的长度 11 (大端),值为“ Some String”

26 ) 0b 表示一个对象序列的结束。

27 )同 1 )

28 ) 04 01 double 类型的表示符号

29 ) 00 00 00 0d ( 13 ) 类型长度(大端)

30 )对象定义标示符( “System.Double” )

  1. 01 00 00 00 ( 1 )包含一个成员

32 ) 07 ( 7 )成员的定义标示符长度

  1. 成员的定义符号 “ m_value”

  2. 00 说明是值类型 06 说明是 double 类型

35 ) <SPAN

Published At
Categories with Web编程
Tagged with
comments powered by Disqus