我如何处理像Nodejs缓冲区结构联合typesC?

我试图parsingNodejs上使用struct uniontypes的缓冲区,我怎样才能在Nodejs上本地处理? 我完全失去了。

typedef union { unsigned int value; struct { unsigned int seconds :6; unsigned int minutes :6; unsigned int hours :5; unsigned int days :15; // from 01/01/2000 } info; }__attribute__((__packed__)) datetime; 

这个联合是一个32位的整value ,或者是那些被分离成6,6,5和15位块的32位的info结构。 我从来没有在Node中与这样的东西进行交互,但是我怀疑在Node中它只是一个数字。 如果是这样的话,你可以得到这样的片断:

 var value = ...; // the datetime value you got from the C code var seconds = value & 0x3F; // mask to just get the bottom six bits var minutes = ((value >> 6) & 0x3F); // shift the bits down by six // then mask out the bottom six bits var hours = ((value >> 12) & 0x1F); // shift by 12, grab bottom 5 var days = ((value >> 17) & 0x7FFF); // shift by 17, grab bottom 15 

如果你不熟悉按位操作,这可能看起来像巫术。 在这种情况下,尝试一下这样的教程(这是C的,但它仍然在很大程度上适用)