转换struct en byte [英] convert struct en byte

查看:67
本文介绍了转换struct en byte的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#将结构转换为字节数组以及如何使函数在c + +中使另一个项目相互作用但不丢失数据

how I can convert a structure to a byte array with c # and how I can make the function reciproce another project in c + + but without losing data

推荐答案

你真的需要学习序列化

http:// en.wikipedia.org/wiki/Serialization#.NET_Framework [ ^ ],

http:// msdn。 microsoft.com/en-us/library/vstudio/ms233843.aspx [ ^ ]。



对于二进制序列化,请参阅:http://msdn.microsoft.com/en-us/library/system.runtime.ser ialization.formatters.binary.binaryformatter.aspx [ ^ ]。



-SA
You really need to learn serialization:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^],
http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx[^].

For binary serialization, please see: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx[^].

—SA

没有标准的方法,因为它取决于你的结构中有什么类型,以及你有什么限制。



例如,如果你不关心最佳速度和空间,你可以将结构流转换为XML(这在C#中相对简单,但在C ++中更复杂)并将XML字符串作为字节数组发送。



请记住,即使简单类型在C#和C ++中的大小也不一定,特别是浮点数会有不同的内存布局。这就是为什么它们经常作为文本传输,或者分解为指数和尾数。
There is no standard method of doing this, as it depends on what types you have in your struct, and what restrictions you have.

For instance, if you''re not concerned with optimal speed and space, you could stream the struct to/from XML (this is relatively trivial in C#, but more complicated in C++) and send the XML string as a byte array.

Keep in mind that even simple types are not necessarily the same size in C# and C++, and floating point numbers in particular will have different memory layout. That''s why they are often transferred as text, or broken up into exponent and mantissa.


>>我如何将结构转换为字节 tbleau 数组

>> how I can convert a structure to a byte tbleau array
struct A
{ int i; };


A a;

char* ptr_to_array = (char*)&a;



你不需要转换结构,它毕竟是只是一个字节数组......



至于.Net(c#)的往返 - 你应该使用System.IntPtr [ ^ ],或者其他可能的东西 - 因为它并不完全清楚你想要什么。 />


祝你好运

Espen Harlinn


You don''t need to convert the structure, it is after all just an array of bytes already ...

As for roundtripping through .Net (c#) - you should perhaps use System.IntPtr[^], or perhaps something else - as it''s not entirely clear what you want.

Best regards
Espen Harlinn


这篇关于转换struct en byte的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆