最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Bytes reversing and swaping in nodejs - Stack Overflow

programmeradmin1浏览0评论

Context

I have a file of binary logs and each log is 7 bytes.

The first 4 bytes is a timestamp (writeUInt32BE), the following 1 byte is an indicator (num 1 or 2 using writeUInt8) and the last 2 bytes are a 4 digit number (writeUInt16BE).

All that works as expected when it comes to writing that data, consuming them and decoding them to text.

Since I'm pushing logs to the file, that means that all the latest logs are at the end of the file. In order to get the latest and avoid consuming all the previous logs from the beginning of the file I implemented (and tested) a reverse stream.

The problem

I'm not able to decode the data in the correct format.

How did I approach the problem:

  1. I stream read the log in reverse order
  2. I swap the bytes to bring them in their original format

Code:

  // a for loop here with a step of +7 
  const byte1 = chunk.readUInt8(i); //first byte of the 4 digit number
  const byte2 = chunk.readUInt8(i + 1); // second of the 4 digit number
  const indicator = chunk.readUInt8(i + 2); // 1 byte total
  const timestampBytes = [
        chunk.readUInt8(i + 3), // Byte 3
        chunk.readUInt8(i + 4), // Byte 4
        chunk.readUInt8(i + 5), // Byte 5
        chunk.readUInt8(i + 6), // Byte 6
  ];

  const 4digitNumber = (byte2 << 8) | byte1; // swap bytes

  // swap timestamp bytes to form a correct UInt32
  const timestamp =
        (timestampBytes[3] << 24) |
        (timestampBytes[2] << 16) |
        (timestampBytes[1] << 8) |
         timestampBytes[0];

Am I doing the swap correctly?

I also tried reversing the bytes and Im still getting incorrect results:

Buffer.from(chunk.slice(i, i + 7).reverse());

current outcome

the outcome logs look mostly correct when it comes to the 4 digit number and the indicator. but the timestamp sometimes is way off (Im expecting dates in the range of 2025 but Im getting some logs that fall to other years like 2027, 1979 etc)

发布评论

评论列表(0)

  1. 暂无评论