|
问:
比如
长度为
用什么方法可以
即:255,123,99,33,55
答:
byte[]
--------------------------------------------------------------------------------------
问:
比如有:
有没有比较简单的方法来连接a
答:
byte[]
a.CopyTo(c,
b.CopyTo(c,
--------------------------------------------------------------------------------------
问:
怎样把Object对象转换成字节数组?
请问怎样把Object对象转换成字节数组,用来在网络上传输?非常感谢!!!!
答:
----------------------
问如何将对象的实体转换为byte[]?
-------------------------
2序列化和反序列化
首先这个类必须是可序列化的,例如DataTable,又或者下面这样:
[Serializable]
public class GradResume
{
/// <summary>
/// 用户ID。
/// </summary>
public string UserID;
/// <summary>
/// 登录名。
/// </summary>
public string UserName;
.......................
序列化和反序列化的代码如下:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//新建类GradResume的实例
GradResume aaa = new GradResume();
aaa.UserID = "123";
aaa.UserName = "eddiezhong";
//序列化对象
BinaryFormatter binaryFormatter = new BinaryFormatter();
System.IO.MemoryStream mStm1 = new System.IO.MemoryStream();
binaryFormatter.Serialize(mStm1,aaa);
byte[] tmpBytes = mStm1.ToArray();
mStm1.Close();
//tmpBytes就可以用来保存数据库了或者写文件
//读数据库或者文件文件重新获得tmpBytes
//反序列化二进制数组
System.IO.MemoryStream mStm2 = new System.IO.MemoryStream(tmpBytes);
mStm2.Position = 0;
object newObj = binaryFormatter.Deserialize(mStm2);
mStm2.Close();
GradResume bbb = (GradResume)newObj;
this.TextBox1.Text = bbb.UserID;
this.TextBox2.Text = bbb.UserName;