Node. js buffer. buffer. byteLength method
- 2020-03-30 04:35:40
- OfStack
Method description:
Gets the length of the string in bytes.
This function differs from string.prototype.length in that it returns the number of characters in the String.
Grammar:
Buffer.byteLength(string, [encoding])
Receiving parameters:
String Characters and
Encoding String encoding, default 'utf8'
Example:
str = 'u00bd + u00bc = u00be';
console.log(str + ": " + str.length + " characters, " +
Buffer.byteLength(str, 'utf8') + " bytes");
// ½ + ¼ = ¾: 9 characters, 12 bytes
Source:
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;
};