How to Use the buffer. Buffer. byteLength method in node. js _ node. js

Source: Internet
Author: User
This article mainly introduces node. buffer in js. buffer. the byteLength method is described in this article. buffer. byteLength method description, syntax, receive parameters, use instances, and implementation source code. For more information, see Method description:

Obtains the length of a string in bytes.

This function differs from String. prototype. length in that the latter returns the number of characters of the String.

Syntax:

The Code is as follows:


Buffer. byteLength (string, [encoding])

Receiving parameters:

String character creation
Encoding string encoding. The default value is 'utf8 ′

Example:

The Code is as follows:


Str = '\ u00bd + \ u00bc = \ u00be ';
Console. log (str + ":" + str. length + "characters," +
Buffer. byteLength (str, 'utf8') + "bytes ");
// Bytes + bytes = bytes: 9 characters, 12 bytes

Source code:

The Code is as follows:


Buffer. byteLength = function (str, enc ){
Var ret;
Str = str + '';
Switch (enc ){
Case 'ascii ':
Case 'binary ':
Case 'Raw ':
Ret = str. length;
Break;
Case 'ucs2 ':
Case 'ucs-2 ':
Case 'utf16le ':
Case 'utf-16le ':
Ret = str. length * 2;
Break;
Case 'hex ':
Ret = str. length >>> 1;
Break;
Default:
Ret = internal. byteLength (str, enc );
}
Return ret;
};

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.