how to correctly get the value of an element by its index from TypedArray?

21 views Asked by At

I use JavaScript and node. I need to read the raw bytes from the file. I create a Uint8array and fill it with data from a file using fs.Read. Then I output the data to the console of the array as a whole, and individual elements. Instead of the values of the elements, I get zeros. But it is clear that the array has non-zero data.


                    var buf1 = new Uint8Array( 4 );

                    fs.open( filePath, 'r+', function ( err, fd ) {
                        if (err) { console.error( err ) };

                        fs.read( fd, buf1, 0, buf1.length, 4,
                            function (err, bytes) {
                                if (err) { console.log(err) };

                        fs.close( fd, function (err) {
                            if (err) { console.log(err) };
                    }) }) });

                    console.log( buf1 );
                    console.log( buf1[0] );
                    console.log( buf1[1] );
                    console.log( buf1[2] );
                    console.log( buf1[3] );

In the console, I see this:

                    var buf2 = new Uint32Array( buf1.buffer );
                    console.log( buf2 );
                    console.log( buf2[0] );

In the console, I see this:

0

There are 0 answers