编程语言
首页 > 编程语言> > nodejs 使用DataView写本地二进制文件时读取问题

nodejs 使用DataView写本地二进制文件时读取问题

作者:互联网

最近遇到这个问题很困惑的问题

使用nodejs写了个本地文件

代码如下

function test() {

    let buffer = new ArrayBuffer(8);
    let u8A = new Uint8Array(buffer);
    let dataView = new DataView(u8A.buffer);
    dataView.setInt8(0, 127);
    dataView.setUint16(1, 256, true);
    dataView.setUint16(3, 11, true); //当这里写入的值大于 127时出现问题
    dataView.setUint16(5, 165, true);//当这里写入的值大于127时会溢出(浏览器读取时)
    let url = "test.bin";
    fs.writeFile(url, dataView, { encoding: 'utf-8' }, function (err) {// 'utf8'
        if (err) {
            console.log('写入文件失败 url: ' + url);//+ " text: " + text
            return;
        }
        console.log('===>写入bin文件成功 url: ' + url);
    });
    console.log("=====>OutBin path:" + path + "  buffer.byteLength: " + buffer.byteLength);
    setTimeout(() => {
        fs.readFile("test.bin", {}, (err, buffer) => {

            console.log("=============>load test.bin buffer: " + buffer + " byteLength: " + buffer.byteLength);

            let dv = new DataView(buffer);
            let a = dv.getInt8(0);
            let b = dv.getUint16(1, true);
            let c = dv.getUint16(3, true);
            let d = dv.getUint16(5, true);

            console.log(`a=${a} b=${b} c=${c} d=${d}`);  //本地这里读取这里没问题
        });
    }, 2000);
}
test();

 

在本地服务起打开index.html时读取生成的该文件时出现溢出问题!!!

<html>

<head>
    <title>www.layabox.com</title>
    <meta charset='utf-8' />
    <meta name='renderer' content='webkit' />
    <meta name='viewport'
        content='width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no' />
    <meta name='apple-mobile-web-app-capable' content='yes' />
    <meta name='full-screen' content='true' />
    <meta name='x5-fullscreen' content='true' />
    <meta name='360-fullscreen' content='true' />
    <meta name='laya' screenorientation='landscape' />
    <meta http-equiv='expires' content='0' />
    <meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1' />
    <meta http-equiv='expires' content='0' />
    <meta http-equiv='Cache-Control' content='no-siteapp' />
</head>

<body>
    <script type="text/javascript">
        function loadLib(url) {
            var script = document.createElement("script");
            script.async = false;
            script.src = url;
            document.body.appendChild(script);
        }
        var request = new XMLHttpRequest();
        request.open("get", "test.bin");
        request.send(null);
        request.responseType = "arraybuffer";
        request.onload = function () {
            console.log("arraybuffer-----", request.response);

            let dv = new DataView(request.response);

            let a = dv.getInt8(0);
            let b = dv.getUint16(1, true);
            let c = dv.getUint16(3, true);//这里值大于127时会产生溢出
            let d = dv.getUint16(5, true);//这里值大于127时会产生溢出

            console.log(`a=${a} b=${b} c=${c} d=${d}`);
        }
    </script>
</body>

</html>

 

 

很久了还是没搞懂为啥会在浏览器读取生成的二进制文件时产生溢出的问题

有高手知道的话不吝赐教,非常感谢

 

标签:console,nodejs,二进制,DataView,buffer,let,url,dv,true
来源: https://www.cnblogs.com/taojianlong/p/16362119.html