node.js中的buffer.toString方法使用說明
更新時間:2014年12月14日 09:24:54 投稿:junjie
這篇文章主要介紹了node.js中的buffer.toString方法使用說明,本文介紹了buffer.toString的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下
方法說明:
將buffer對象轉(zhuǎn)換成指定的字符編碼的字符串。
語法:
復制代碼 代碼如下:
buffer.toString([encoding], [start], [end])
接收參數(shù):
encoding 轉(zhuǎn)換成字符串后的字符編碼,默認為 ‘utf8′
start buffer 轉(zhuǎn)換的起始位置,默認為 0
end buffer 轉(zhuǎn)換的結(jié)束位置,默認為buffer長度
例子:
復制代碼 代碼如下:
var b = new Buffer(50);
console.log(b);
var c = b.toString('base64',0,10);
console.log(c);
源碼:
復制代碼 代碼如下:
// toString(encoding, start=0, end=buffer.length)
Buffer.prototype.toString = function(encoding, start, end) {
var loweredCase = false;
start = start >>> 0;
end = util.isUndefined(end) ? this.length : end >>> 0;
if (!encoding) encoding = 'utf8';
if (start < 0) start = 0;
if (end > this.length) end = this.length;
if (end <= start) return '';
while (true) {
switch (encoding) {
case 'hex':
return this.hexSlice(start, end);
case 'utf8':
case 'utf-8':
return this.utf8Slice(start, end);
case 'ascii':
return this.asciiSlice(start, end);
case 'binary':
return this.binarySlice(start, end);
case 'base64':
return this.base64Slice(start, end);
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return this.ucs2Slice(start, end);
default:
if (loweredCase)
throw new TypeError('Unknown encoding: ' + encoding);
encoding = (encoding + '').toLowerCase();
loweredCase = true;
}
}
};
您可能感興趣的文章:
- 使用node.js中的Buffer類處理二進制數(shù)據(jù)的方法
- Node.js中使用Buffer編碼、解碼二進制數(shù)據(jù)詳解
- Node.js Windows Binary二進制文件安裝方法
- node.js中Buffer緩沖器的原理與使用方法分析
- Node.js Buffer模塊功能及常用方法實例分析
- 詳解如何在Node.js的httpServer中接收前端發(fā)送的arraybuffer數(shù)據(jù)
- Node.js Buffer用法解讀
- 關(guān)于Node.js中Buffer的一些你可能不知道的用法
- 淺談Node.js:Buffer模塊
- Node.js實用代碼段之正確拼接Buffer
- Node.js實用代碼段之獲取Buffer對象字節(jié)長度
- node.js中的buffer.copy方法使用說明
- node.js中的buffer.fill方法使用說明
- node.js中的buffer.length方法使用說明
- node.js中的buffer.toJSON方法使用說明
- node.js中的buffer.Buffer.isEncoding方法使用說明
- node.js中的buffer.Buffer.isBuffer方法使用說明
- node.js中的buffer.Buffer.byteLength方法使用說明
- node.js中的buffer.slice方法使用說明
- node.JS二進制操作模塊buffer對象使用方法詳解
相關(guān)文章
node.js中使用node-schedule實現(xiàn)定時任務實例
這篇文章主要介紹了node.js中使用node-schedule實現(xiàn)定時任務實例,包括安裝方法和4種使用例子,需要的朋友可以參考下2014-06-06

