d 语言的 struct 到 ubyte[] 或 ubyte[] 到 struct

发布于 2025-01-08 18:09:28 字数 352 浏览 0 评论 0原文

在D语言中如何执行struct到ubyte[]或者ubyte[]到struct,请兄弟帮忙解答这个问题,谢谢!

如果结构体包含 stringchar [] 该怎么办?

比如这样的结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山人契 2025-01-15 18:09:28

我认为标准库中没有任何东西可以自动将结构序列化和反序列化为字节流。 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.

ぺ禁宫浮华殁 2025-01-15 18:09:28

要转换原始数据,建议的习惯用法如下:

struct_type* s = new struct_type;
ubyte[] ub = cast(ubyte[]) s[0..1];
struct_type* s2 = cast(struct_type*) ub.ptr;

但这不会处理字符串和指针的序列化。您需要手动或通过库来完成此操作。

To convert the raw data, the suggested idiom is like this:

struct_type* s = new struct_type;
ubyte[] ub = cast(ubyte[]) s[0..1];
struct_type* s2 = cast(struct_type*) ub.ptr;

This will not handle serialization of strings and pointers, though. You will need to do that manually or with a library.

赠我空喜 2025-01-15 18:09:28

@Dev Wolf:你必须自己编写序列化/反序列化。除了 Cyber​​Shadow 提到的 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.

情绪操控生活 2025-01-15 18:09:28
struct UserLogin
{
  align(1): 
  ushort ClientId; 
  int AccectId; 
  char[10] LoginUid; 
  char[10] LoginPwd; 
}

UserLogin* readByteToStruct = cast(UserLogin*)ne.data;

将能够正确地获取数据...

struct UserLogin
{
  align(1): 
  ushort ClientId; 
  int AccectId; 
  char[10] LoginUid; 
  char[10] LoginPwd; 
}

UserLogin* readByteToStruct = cast(UserLogin*)ne.data;

Will be able to properly take the data...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文