Write a node program that prints a buffer object containing the string “bytewiser” using console.log.
1
2
3
var str = 'bytewiser';
var buffer = new Buffer(str);
console.log(buffer);
bytewiser-exercise-2
Given an unknown number of bytes passed via process.argv, create a buffer from them and output a hexadecimal encoded representation of the buffer.
1
2
3
4
5
6
7
var array = process.argv.slice(2);
var buffer = new Buffer(array);
console.log(buffer.toString('hex'));
// 官方答案
// var bytes = process.argv.slice(2).map(function(arg) { returnparseInt(arg) })
// console.log(new Buffer(bytes).toString('hex'))
bytewiser-exercise-3
Write a program that takes the first buffer written to process.stdin, updates all instances of . with ! and then logs out the updated buffer object.
1
2
3
4
5
6
7
8
9
// 监听用户输入,将 . 替换为 ! 输出
process.stdin.on('data', function(buffer){
for (var i = 0; i < buffer.length; i++) {
// 0x2e 对应为 .
// 0x2e 对应为 !
if (buffer[i] === 0x2e) buffer[i] = 0x21
}
console.log(buffer);
});
bytewiser-exercise-4
The argument given to you from process.argv[2] will be a path to a file. Read this file and split it by newline characters (‘\n’). You should log one Buffer per line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var fs = require('fs');
var file = process.argv[2];
fs.readFile(file, function(err, data){
if (err) {
throw err;
}
vararray = data.toString().split('\n');
var len = array.length;
var buffer;
for (var i = 0; i < len; i++) {
buffer = new Buffer(array[i]);
console.log(buffer);
};
});
// 官方答案
// var fs = require('fs')
// var file = fs.readFileSync(process.argv[2])
// var offset = 0
// for (var i = 0; i < file.length; i++) {
// if (file[i] === 10) {
// console.log(file.slice(offset, i))
// i++
// offset = i
// }
// }
// console.log(file.slice(offset, i))
bytewiser-exercise-5
Write a program that combines all of the buffers from process.stdin and then writes the single big buffer out to the console.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var buffers = [];
process.stdin.on('data', function (chunk) {
var buffer = new Buffer(chunk);
buffers.push(buffer);
});
process.stdin.on('end', function () {
varresult = Buffer.concat(buffers);
console.log(result);
});
// 官方答案
// var buffers = [];
// process.stdin.on('readable', function() {
// var chunk = process.stdin.read();
// if (chunk !== null) {
// buffers.push(chunk);
// }
// });
// process.stdin.on('end', function() {
// console.log(Buffer.concat(buffers));
// });
bytewiser-exercise-6
Read the first buffer from process.stdin, copy all bytes into a Uint8Array and then log out a JSON stringified representation of the typed array.
1
2
3
4
5
6
7
8
9
10
11
process.stdin.on('data', function (chunk) {
var uInt8array = new Uint8Array(chunk);
varresult = JSON.stringify(uInt8array);
console.log(result);
});
// 官方答案
// process.stdin.once('data', function(buff) {
// var ui8 = new Uint8Array(buff)
// console.log(JSON.stringify(ui8))
// })
bytewiser-exercise-7
Take the integer from process.argv[2] and write it as the first element in a single element Uint32Array. Then create a Uint16Array from the Array Buffer of the Uint32Array and log out to the console the JSON stringified version of the Uint16Array.
1
2
3
4
5
6
7
8
9
10
11
12
13
varint = parseInt(process.argv[2]);
var uint32array = new Uint32Array(1);
uint32array[0] = int;
var uint16array = new Uint16Array(uint32array.buffer);