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;
};