node.jsbufferdword

What is the equivalent of DWORD in NodeJS?


I'm trying to rewrite a protocol in NodeJS from documentation written for C#.

I'm reading from an index file and here's the description of how the buffer is laid out.

enter image description here enter image description here

From what I've read, DWORD is just a 32 bit unsigned integer. However, whenever I read the length bytes with indexFileBuffer.readUInt32BE(offset) I get a much longer length than expected.

Apparently readUInt32BE() is not the equivalent of dword or dword must be handled differently based on some criteria.

Any insight into how to properly calculate the value would be greatly appreciated.


Solution

  • The proper functions to get a DWORD (unsigned 32 bit integer) from a buffer are either:

    buf.readUInt32BE(offset)
    

    or

    buf.readUInt32LE(offset)
    

    You do need to know whether your data is little endian or big endian byte ordering in order to select the right option of the two.


    If you're using this and it isn't getting you the value you expect, then one of three things must be going on:

    1. The data you expect to be in the buffer isn't what is there
    2. You aren't using the right "endian" version
    3. Your offset into the buffer where you're reading isn't correct

    FYI, Intel uses little endian byte ordering, ARM uses big endian. More on "endinan-ness" here.