Javascript二进制文件读取

2023-12-01

From here:

_shl: function (a, b){
        for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
        return a;
    },

_readByte: function (i, size) {
        return this._buffer.charCodeAt(this._pos + size - i - 1) & 0xff;
    },

_readBits: function (start, length, size) {
        var offsetLeft = (start + length) % 8;
        var offsetRight = start % 8;
        var curByte = size - (start >> 3) - 1;
        var lastByte = size + (-(start + length) >> 3);
        var diff = curByte - lastByte;

        var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);

        if (diff && offsetLeft) {
            sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight; 
        }

        while (diff) {
            sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight);
        }

        return sum;
    },

此代码执行二进制文件读取。不幸的是,这段代码没有记录在案。 我想了解它是如何工作的。 (特别是 _readBits 和 _shl 方法) _readBits 中 offsetright 的作用是什么?还有 curByte 和 lastByte: 我是这样想的:

_readBits(0,16,2) curByte 变为 1。lastByte 变为 0。为什么lastByte 小于 curByte?或者我犯了一个错误?在哪里?请帮忙!


  • _readByte(i, size): size是以字节为单位的缓冲区长度,i is the 反向索引(从缓冲区末尾开始的索引,而不是从开头开始)要读取的字节。
  • _readBits(start, length, size): size是以字节为单位的缓冲区长度,length是您要读取的位数,startindex您要读取的第一位。
  • _shl(a, b):转换读取的字节a根据其索引得出实际值b.

Because _readByte使用反向索引,所以lastByte少于curByte.

offsetLeft and offsetRight用于删除不相关的位(不在读取范围内的位)lastByte and curByte分别。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript二进制文件读取 的相关文章

随机推荐