Search code examples
ddmd

struct to ubyte[] or ubyte[] to struct for d language


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!


Solution

  • 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.