>>node>>fs.readfile (' Finnish.txt ',function(err,data) {console.log (data);});//Output string is not what we want>>fs.readfile (' Finnish.txt ',function(err, data) {Console.log (data.tostring ());});//Ouptu is ok/*Encoding*/>>fs.readfile (' finnish.txt ', ' UTF8 ',function(err,data) {console.log (data);});//Ouptu is ok>>str = ' \u0048\u0065\u006c\u006f \u0057\u006f\u0072\u006c\u0064\u0021 ';' Hello World 'Str.length12buffer.bytelength (str,' UTF8 ')12But the buffer.bytelength and str.length is not equal!! ------>>STR = ' \U00BD + \U00BC = \u00be ';' 1/2 + 1/4 = 3/4 '; Str.length9buffer.bytelength (str,' UTF8 ')----->>BUF =NewBuffer (5)>>buf.write (' Hello wolrd ');>>buf.tostring ()' Hello ' >>buf.write (' Hello ', 2); Hehel>>buf.write (' xxxx ', 2, 1);>>buf.tostring () Hexel>>buf.write (' xxxx ', 2, 2);>>buf.tostring () Hexxel>>buf.write (' yyyy ', 2, 1, ' UTF8 ')>>buf.tostring () Heyel-----BUF1=NewBuffer (' 1234 ') Buf2=NewBuffer (' 0123 ') buf3=NewBuffer (' 1234 ') Buf1.compare (BUF2)1//Not equalBuf1.compare (BUF3)0//Equalbuf1.equals (BUF2)false>>vararr =[Buf1, Buf2]>>Arr.sort (buffer.compare) [<buffer, 33>, <buffer 34> ]>>Buf.tojson () {type:' Buffer ', data: [104, 101, 121, 101, 108]}>> buf =NewBuffer (' Hello world! ');>> buf2 0 Buf.slice (0,3) buf2.tostring ();' Hel '
In this lesson, we cover the node. JS Buffer object in detail. Not only would you learn that the buffer object was a reference to a memory space outside of the V8 engine, but you'll lea RN Practical methods to access it, modify it, and convert it to standard Javascript objects The can be used by your code. Examples and discussion is provided for using the ToString () method, determining the byte length of the buffer, writing To a buffer, how to avoid truncating data, comparing buffers for equality using both compare () and Equals (), and copying B Uffers using Slice ().
[node. js] node. js Buffers