How to pack a struct to a ubyte[] in a more "The D way" style ?

Binghoo Dang dangbinghoo at gmail.com
Mon Dec 18 08:45:32 UTC 2017


hi Davis,

I read the std.bitmanip, and I tried to test the code like below:

```
import std.stdio;
import std.array;
import std.bitmanip;
void main(string[] args)
{
     align(1) struct c {
         ushort c1;
         uint c2;
         //ubyte[8] c3;
     }
     ubyte[] buffer;
     auto ap = appender(&buffer);
     uint a = 0x11223344;
     ushort b = 0x6677;
     ap.append!uint(a);
     ap.append!ushort(b);

     c cobj;
     cobj.c1 = 0xEEFF;
     cobj.c2 = 0xDEADBEAF;
     //cobj.c3 = [0x12, 0x34, 0x56, 0x78];
     ap.append(cobj);

     ubyte[3] d = [0xAA, 0xBB, 0xCC];
     ap.append(d);

     foreach(e; buffer) {
         writef("%02X ", e);
     }
}
```
For compiling this code, I got error like this

> onlineapp.d(22): Error: template std.bitmanip.append cannot 
> deduce function from >argument types !()(RefAppender!(ubyte[]), 
> c), candidates are:
>/dlang/dmd/linux/bin64/../../src/phobos/std/bitmanip.d(3623):    
>    std.bitmanip.append(T, Endian endianness = Endian.bigEndian, 
>R)(R range, T value) >if (canSwapEndianness!T && 
>isOutputRange!(R, ubyte))
>onlineapp.d(25): Error: template std.bitmanip.append cannot 
>deduce function from >argument types !()(RefAppender!(ubyte[]), 
>ubyte[3]), candidates are:
>/dlang/dmd/linux/bin64/../../src/phobos/std/bitmanip.d(3623):    
>    std.bitmanip.append(T, Endian endianness = Endian.bigEndian, 
>R)(R range, T value) >if (canSwapEndianness!T && 
>isOutputRange!(R, ubyte))

It seems that the appending is just allow using the fundamental 
types like uint, ushort, it does not support struct, and can't 
support dynamic array too. As the hardware protocol is a 
dynamic-length style, so I also need to support append array or 
another buffer directly.

It seems that I can't get the point how to using the bitmanip to 
do the packing I need, and also I have no idea what the unpacking 
will look like.

Thanks!


More information about the Digitalmars-d-learn mailing list