d 语言的 struct 到 ubyte[] 或 ubyte[] 到 struct
在D语言中如何执行struct到ubyte[]
或者ubyte[]
到struct,请兄弟帮忙解答这个问题,谢谢!
如果结构体包含 string
或 char []
该怎么办?
比如这样的结构:
struct UserLogin
{
align(1):
ushort ClientId;
int AccectId;
string LoginUid;
string LoginPwd;
}
注意我在socket中的应用!
How the implementation of the struct in the D language to ubyte []
or ubyte []
to the struct, please brothers help answer this question, thank you!
If a struct contains the string
or char []
what to do?
For example, such a structure:
struct UserLogin
{
align(1):
ushort ClientId;
int AccectId;
string LoginUid;
string LoginPwd;
}
Attention to my application in the socket!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为标准库中没有任何东西可以自动将结构序列化和反序列化为字节流。
std.stream
对各种基本类型执行此操作,但不适用于整个结构。 Apache Thrift 支持即将推出。在第 3 方解决方案中,您可以查看 Orange 序列化库。I don't think there's anything in the standard library to automatically serialize and deserialize structures to byte streams.
std.stream
does that for a variety of basic types, but not entire structs. Apache Thrift support is on the way. Among 3rd-party solutions, you can have a look at the Orange serialization library.要转换原始数据,建议的习惯用法如下:
但这不会处理字符串和指针的序列化。您需要手动或通过库来完成此操作。
To convert the raw data, the suggested idiom is like this:
This will not handle serialization of strings and pointers, though. You will need to do that manually or with a library.
@Dev Wolf:你必须自己编写序列化/反序列化。除了 CyberShadow 提到的 Orange 之外,您还可以实现 Thrift 协议: http://klickverbot.at/code /gsoc/thrift/ 。我记得有些人也致力于 Google Protocol Buffer 的实现。
@Dev Wolf : You have to write serialization/deserialization yourself. Apart from the Orange mentioned by CyberShadow you also have the Thrift protocol implementation : http://klickverbot.at/code/gsoc/thrift/ . I remember some guys worked on Google Protocol Buffer implementation as well.
将能够正确地获取数据...
Will be able to properly take the data...