Method description:
Gets the byte length of the string.
The difference between this function and String.prototype.length is that the latter returns the number of characters of the string.
grammar:
The code copy is as follows:
Buffer.byteLength(string, [encoding])
Receive parameters:
string character creation
encoding string encoding, default to 'utf8'
example:
The code copy is as follows:
str = '/u00bd + /u00bc = /u00be';
console.log(str + ": " + str.length + " characters, " +
Buffer.byteLength(str, 'utf8') + " bytes");
// ½ + ¼ = ¾: 9 characters, 12 bytes
Source code:
The code copy 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 return;
};