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

2017-12-21 Thread Binghoo Dang via Digitalmars-d-learn
hi Davis, Thanks for your great help to me! Yeah, the library may had a design principle when it was designed, as you can see the buffer appender is not that suitable for an application-defined structured data packing. And after I turn to the bitfield, I then got another trouble: The

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

2017-12-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, December 18, 2017 08:45:32 Binghoo Dang via Digitalmars-d-learn wrote: > 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 { >

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

2017-12-21 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, December 18, 2017 05:32:02 Binghoo Dang via Digitalmars-d-learn wrote: > Thanks for your idea. > > On Monday, 18 December 2017 at 05:08:24 UTC, Jonathan M Davis > > wrote: > >> ubyte[] makeCMDPacket(RCPCmd cmd, in ubyte[] data) > >> { > >> > >>

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

2017-12-18 Thread Binghoo Dang via Digitalmars-d-learn
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 =

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

2017-12-17 Thread Binghoo Dang via Digitalmars-d-learn
Thanks for your idea. On Monday, 18 December 2017 at 05:08:24 UTC, Jonathan M Davis wrote: ubyte[] makeCMDPacket(RCPCmd cmd, in ubyte[] data) { this.preamble = RCPPKT_PRE; this.hdr.msgtype = RCPMSG_CMD; this.hdr.cmdcode = cast (ubyte)

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

2017-12-17 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, December 18, 2017 03:00:47 Binghoo Dang via Digitalmars-d-learn wrote: > hi, > > I'm a dlang newbie, and I recently made a program that interacts > with an RFID Reader, it's basically a Serial-Port communication > model. And after 2 days, I finally got a working code like these: > >

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

2017-12-17 Thread Binghoo Dang via Digitalmars-d-learn
hi, I'm a dlang newbie, and I recently made a program that interacts with an RFID Reader, it's basically a Serial-Port communication model. And after 2 days, I finally got a working code like these: ``` enum RCPCmd{ RCPCMD_GET_REGION = 0x06, RCPCMD_SET_REGION = 0x07,