[node. js] node. js Buffers

Source: Internet
Author: User
Tags readfile

>>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!! ------&GT;&GT;STR = ' \U00BD + \U00BC = \u00be ';' 1/2 + 1/4 = 3/4 '; Str.length9buffer.bytelength (str,' UTF8 ')-----&GT;&GT;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&gt, <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.