再请教个问题 object 怎么转成 一个byte []

witchboy001 2011-05-30 04:26:15
object 是由一个struct 赋值来的 内容是一个网络消息 要转成byte[] 发送 不知道如何把一个object 转成byte[]
...全文
565 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
witchboy001 2011-06-03
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 kingdom_0 的回复:]
引用 31 楼 witchboy001 的回复:

先这样了吧 结贴给分
这个问题研究了一天啊……
[/Quote]

不是啊 我都已经改好了 回复晚了么
witchboy001 2011-05-31
  • 打赏
  • 举报
回复
但是这样的话 每个消息里面都要加一个转换函数 我这里的消息又很多 有没有更简单的方法?
lion0327 2011-05-31
  • 打赏
  • 举报
回复
这样的话不是每个消息都要加转换函数,消息多的话会不会很麻烦,有没有更简化的方法呢
witchboy001 2011-05-31
  • 打赏
  • 举报
回复


public static void SetStruct(object o, int offset)
{
AuthenAccount_Req_Cmd aarc ;
List<object> list = new List<object>();
if (o is AuthenAccount_Req_Cmd)
{
aarc = (AuthenAccount_Req_Cmd)o;
}
else
{
throw new Exception("o is not a struct type");
return;
}
// list.Add(aarc.uHead);
foreach (var a in aarc.pstrUserName)
list.Add(a);
foreach (var a in aarc.pstrUserPassword)
list.Add(a);
// list.Add(aarc.wGroup);

byte[] c = BitConverter.GetBytes(aarc.wGroup);
foreach (var a in c)
list.Add(a);

byte[] bt=new byte[list.Count];
for (int i = 0; i < list.Count; i++)
{
byte b = Convert.ToByte(list[i]);
bt[i] = b;
}
}







这样应该可以了
witchboy001 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 yunhaic 的回复:]
C# code

using System.Runtime.Serialization.Formatters.Binary; //BinaryFormatter
using System.IO; //MemoryStream

///<summary>
/// 序列化
/// </summary>
/// <……
[/Quote]

感谢 写的很详细 序列化是比较方便 但我这里似乎用起来有点问题
witchboy001 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 kingdom_0 的回复:]
昨天没来得及回答你,下班了。给你写个,参考下吧

C# code

public static void SetStruct(object o, int offset)
{
AuthenAccount_Req_Cmd aarc ;
List<object> list = new List<object>();
……
[/Quote]

谢谢 正在研究 还要把多字节类型 分别转成单个字节存入list 比如int short之类
  • 打赏
  • 举报
回复

using System.Runtime.Serialization.Formatters.Binary; //BinaryFormatter
using System.IO; //MemoryStream

///<summary>
/// 序列化
/// </summary>
/// <param name="data">要序列化的对象</param>
/// <returns>返回存放序列化后的数据缓冲区</returns>
public static byte[] Serialize(object data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream();
formatter.Serialize(rems, data);
return rems.GetBuffer();
}

/// <summary>
/// 反序列化
/// </summary>
/// <param name="data">数据缓冲区</param>
/// <returns>对象</returns>
public static object Deserialize(byte[] data)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream rems = new MemoryStream(data);
data = null;
return formatter.Deserialize(rems);
}
kingdom_0 2011-05-31
  • 打赏
  • 举报
回复
昨天没来得及回答你,下班了。给你写个,参考下吧

public static void SetStruct(object o, int offset)
{
AuthenAccount_Req_Cmd aarc ;
List<object> list = new List<object>();
if (o is AuthenAccount_Req_Cmd)
{
aarc = (AuthenAccount_Req_Cmd)o;
}
else
{
throw new Exception("o is not a struct type");
return;
}
list.Add(aarc.uHead);
foreach (var a in aarc.pstrUserName)
list.Add(a);
foreach (var a in aarc.pstrUserPassword)
list.Add(a);
list.Add(aarc.wGroup);
byte[] bt=new byte[list.Count];
for (int i = 0; i < list.Count; i++)
{
byte b = Convert.ToByte(list[i]);
bt[i] = b;
}
}
kingdom_0 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 witchboy001 的回复:]

先这样了吧 结贴给分
[/Quote]这个问题研究了一天啊……
witchboy001 2011-05-31
  • 打赏
  • 举报
回复
先这样了吧 结贴给分
cmmrpp 2011-05-30
  • 打赏
  • 举报
回复
Convert.ToByte(r);
witchboy001 2011-05-30
  • 打赏
  • 举报
回复
再顶顶 别沉了
witchboy001 2011-05-30
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 jimh 的回复:]
二进制序列化就可以写进stream里面,然后read到byte[].
[/Quote]

序列化是不是消息会变长? 还有是不是服务器端也要修改? 现在我做的这个项目服务器是用之前的一个项目改的 服务器是c++写的 而且还是跑在linux上的
jimh 2011-05-30
  • 打赏
  • 举报
回复
二进制序列化就可以写进stream里面,然后read到byte[].
witchboy001 2011-05-30
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 mjp1234airen4385 的回复:]
以下代码是转贴:
//struct转换为byte[]
public static byte[] StructToBytes(object structObj)
{
int size = Marshal.SizeOf(structObj);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.……
[/Quote]

谢了 但是这段代码我用不了
事实是这样的 之前我也是用的这段 但当我用unity导出webplayer以后因为安全机制的缘故 marshal函数基本不能用了 不然就容易解决了 唉 麻烦啊

下面是相关网址
http://unity3d.com/support/documentation/Manual/Security%20Sandbox.html
witchboy001 2011-05-30
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 kingdom_0 的回复:]
C# code
PacketData.SetStruct(msg, 0);

msg是一个结构体对象,在这个方法中,你想给结构体赋值?用什么赋值?
[/Quote]

哦 是这样的 msg 是赋值好了传进去的 在SetStruct里面再传给一个 byte[] 所以我一直问要怎么把object 转成 byte[] 有办法吗?
mjp1234airen4385 2011-05-30
  • 打赏
  • 举报
回复
以下代码是转贴:
//struct转换为byte[]
public static byte[] StructToBytes(object structObj)
{
int size = Marshal.SizeOf(structObj);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structObj, buffer, false);
byte[] bytes = new byte[size];
Marshal.Copy(buffer, bytes, 0, size);
return bytes;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}





public static object BytesToStruct(byte[] bytes, Type strcutType)
{
int size = Marshal.SizeOf(strcutType);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, 0, buffer, size);
return Marshal.PtrToStructure(buffer, strcutType);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
kingdom_0 2011-05-30
  • 打赏
  • 举报
回复
PacketData.SetStruct(msg, 0);

msg是一个结构体对象,在这个方法中,你想给结构体赋值?用什么赋值?
witchboy001 2011-05-30
  • 打赏
  • 举报
回复
顶一顶哦
witchboy001 2011-05-30
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 effun 的回复:]
在发送方,用BinaryWriter把对象的属性一个个写到MemoryStream里,就可以得到byte[]了。在接收方,按原先写入的顺序用BinaryReader再一个个地读出来赋值给对象。
[/Quote]

唉 服务器端是用之前的一个改的 是c++写的
加载更多回复(13)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧