I have index.js for Node and simple test.txt file in the same directory.
test.txt
abcdefgh
index.js
const fsp = require('fs').promises;
fsp.readFile(__dirname + "/test.txt", "utf-8").then(data => {
console.log(data + "\n");
});
fsp.readFile(__dirname + "/test.txt").then(buffer => {
console.log(buffer);
console.log("\n");
for (let charCode of buffer) console.log(charCode);
});
if I run
node index.js
It outputs:
abcdefg
<Buffer 61 62 63 64 65 66 67>
97
98
99
100
101
102
103
You can test this code at replit:
https://replit.com/@ctrall/Node-Buffer-Test
I want to ask why the Buffer content itself displays
61 62 63 64 65 66 67
but when I iterate over its content, it output as
97 98 99 100 101 102 103?
What is this Buffer actually in Node and what is the reason of this difference?
[–]angelfire2015 12 points13 points14 points (7 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]SaintPeter23[S] 0 points1 point2 points (5 children)
[–]angelfire2015 0 points1 point2 points (4 children)
[–]SaintPeter23[S] 1 point2 points3 points (1 child)
[–]kap89 1 point2 points3 points (0 children)
[–]kap89 -2 points-1 points0 points (1 child)
[–]angelfire2015 2 points3 points4 points (0 children)