Description of the buffer. Buffer. byteLength method in node. js, node. jsbuffer
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:
Copy codeThe Code is as follows:
Buffer. byteLength (string, [encoding])
Receiving parameters:
String character creation
Encoding string encoding. The default value is 'utf8 ′
Example:
Copy codeThe 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:
Copy codeThe 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;
};