After running this code:
var arr = new Uint32Array(16);
for (var i=0; i<16; ++i) arr[i] = i;
fs.writeFileSync("arr",new Uint8Array(arr).buffer);
console.log([].slice.call(new Uint32Array(fs.readFileSync("arr"))));
The expected output is:
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
But, instead, it produces this output:
[ 91, 111, 98, 106, 101, 99, 116, 32, 65, 114, 114, 97, 121, 66, 117, 102, 102, 101, 114, 93 ]
A hexdump of the arr file shows this:
0000000 5b 6f 62 6a 65 63 74 20 41 72 72 61 79 42 75 66
0000010 66 65 72 5d
Why does the produced output not match the expected output?
The buffer property of a TypedArray (Uint8Array in this case) is an ArrayBuffer, which is not the same as a node.js Buffer. If you try to read/write an ArrayBuffer to a file when the fs module is expecting a node.js Buffer, it's not going to work.
However, you can convert between the two any number of ways. The simplest change to your code to make it work as expected would be to simply initialize a Buffer from arr instead of trying to use the .buffer property:
var arr = new Uint32Array(16);
for (var i=0; i<16; ++i) arr[i] = i;
fs.writeFileSync("arr", new Buffer(arr)); // <-- HERE
console.log([].slice.call(new Uint32Array(fs.readFileSync("arr"))));
Outputs:
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
And, for completeness, a hex dump of the arr file looks like:
0000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f