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 -
JavaScript文字列をブール値に変換します
JavaScriptで文字列をブール値に変換するためのコードは次のとおりです- 例 <!DOCTYPE html> <html> <head> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } </style> </head> <body> <h1>Converting strin
-
文字列をJavaScriptオブジェクトに変換する方法は?
以下は、文字列をJavaScriptオブジェクトに変換するコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> &nbs