在 JavaScript 中將緩衝區轉換為可讀字串?
為此,使用 toString(‘utf8’) 的概念。以下為程式碼 −
在下面的程式碼中,對緩衝區進行了詳細說明。
示例
var actualBufferObject = Buffer.from('[John Smith]', 'utf8')
console.log("The actual buffer object=");
console.log(JSON.stringify(actualBufferObject))
console.log("Get back the original object=");
console.log(actualBufferObject.toString('utf8'));
var myObjectValue = '[John Smith]';
console.log("The data you are getting from the buffer is equal to ASCII
code equivalent...")
for (var counter = 0; counter < myObjectValue.length; counter++) {
console.log("The ascii value of " + myObjectValue[counter] + " is ="
+ (myObjectValue.charCodeAt(counter)));
}要執行以上程式,需要使用以下命令 −
node fileName.js.
此處,我的檔名是 demo197.js。
輸出
這將產生以下輸出 −
PS C:\Users\Amit\javascript-code> node demo197.js
The actual buffer object=
{"type":"Buffer","data":[91,74,111,104,110,32,83,109,105,116,104,93]}
Get back the original object=
[John Smith]
The data you are getting from the buffer is equal to ASCII code equivalent...
The ascii value of [ is =91
The ascii value of J is =74
The ascii value of o is =111
The ascii value of h is =104
The ascii value of n is =110
The ascii value of is =32
The ascii value of S is =83
The ascii value of m is =109
The ascii value of i is =105
The ascii value of t is =116
The ascii value of h is =104
The ascii value of ] is =93
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP