如何在Nodejs Buffer上处理类似struct union类型的C?

Cai*_*uts 6 javascript node.js

我正在尝试解析使用struct union类型的Nodejs上的缓冲区,我如何在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;
Run Code Online (Sandbox Code Playgroud)

Mat*_*eer 7

这个联合是一个32位整数value,或者info是32位分隔成6,6,5和15位块的结构.我从来没有在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
Run Code Online (Sandbox Code Playgroud)

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