question about bitfields to decode websocket header

lithium iodate whatdoiknow at doesntexist.net
Wed Nov 7 14:22:43 UTC 2018


On Wednesday, 7 November 2018 at 13:05:49 UTC, test wrote:
> I am confused about the bitfields order.
>
> mixin(bitfields!(
> 	bool, 	"fin",     	1,
> 	bool,   "rsv1", 	1,
> 	bool,   "rsv2", 	1,
> 	bool,   "rsv3", 	1,
> 	Opcode, "opcode", 	4,
> 		
> 	bool,   "mask", 	1,
> 	ubyte,  "_size", 	7,
> ));
>
>
> output for first byte is 10000001 ,  the 2st byte 10000011
>
> my code output:  opcode=8 mask=true size=65
>
> the real size is 3 byte, and opcode is 1;
>
> how to fix this ?

The bitfields start with the least significant bits:
fin -> 1
rsv1 -> 0
rsv2 -> 0
rsv3 -> 0
opcode -> 1000 = 8

mask -> 1
_size -> 1000001 = 65

This order will likely be what you want:
mixin(bitfields!(
     opcode, "opcode", 4,
     bool,   "rsv3",   1,
     bool,   "rsv2",   1,
     bool,   "rsv1",   1,
     bool,   "fin",    1,

     ubyte,  "_size",  7,
     bool,   "mask",   1,
));

Also beware of endianness when mapping bytes to it.


More information about the Digitalmars-d-learn mailing list