Home > Web Front-end > JS Tutorial > Instructions for using the buffer.Buffer.byteLength method in node.js_node.js

Instructions for using the buffer.Buffer.byteLength method in node.js_node.js

WBOY
Release: 2016-05-16 16:27:44
Original
1941 people have browsed it

Method description:

Get the byte length of the string.

The difference between this function and String.prototype.length is that the latter returns the number of characters in the string.

Grammar:

Copy code The code is as follows:

Buffer.byteLength(string, [encoding])

Receive parameters:

string character creation
encoding String encoding, the default is ‘utf8′

Example:

Copy code The code is as follows:

str = 'u00bd u00bc = u00be';
console.log(str ": " str.length " characters, "
Buffer.byteLength(str, 'utf8') " bytes");
// ½ ¼ = ¾: 9 characters, 12 bytes

Source code:

Copy 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;
};
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template