I get an array of numbers as a response from remote command execution (using ssh2). How do I convert it to a string?
[97,112,112,46,106,115,10,110,111,100,101,46,106,115,10]
-
3You mean converting each number to the corresponding ASCII character ?jillro– jillro2014年02月20日 19:51:16 +00:00Commented Feb 20, 2014 at 19:51
-
possible duplicate of Convert character to ASCII code in JavascriptAsh– Ash2014年02月20日 20:07:16 +00:00Commented Feb 20, 2014 at 20:07
-
@Ashish Nope. Actually it's the opposite.Bali Balo– Bali Balo2014年02月20日 20:13:30 +00:00Commented Feb 20, 2014 at 20:13
-
@Bali Balo,Ya right ! but in that, this is also define that how to do the same.Ash– Ash2014年02月20日 20:17:04 +00:00Commented Feb 20, 2014 at 20:17
-
read @Török Gábor comment in that question.Ash– Ash2014年02月20日 20:18:50 +00:00Commented Feb 20, 2014 at 20:18
5 Answers 5
var result = String.fromCharCode.apply(null, arrayOfValues);
Explanations:
String.fromCharCode can take a list of char codes as argument, each char code as a separate argument (for example: String.fromCharCode(97,98,99)).
apply allows to call a function with a custom this, and arguments provided as an array (in contrary to call which take arguments as is). So, as we don't care what this is, we set it to null (but anything could work).
In conclusion, String.fromCharCode.apply(null, [97,98,99]) is equivalent to String.fromCharCode(97,98,99) and returns 'abc', which is what we expect.
Comments
It depends on what you want and what you mean.
Option One: If you want to convert the text to ASCII, do this:
var theArray = [97,112,112,46,106,115,10,110,111,100,101,46,106,115,10];
theString = String.fromCharCode.apply(0, theArray);
(Edited based on helpful comments.)
Produces:
app.js
node.js
Option Two: If you just want a list separated by commas, you can do .join(','):
var theArray = [97,112,112,46,106,115,10,110,111,100,101,46,106,115,10];
var theString = theArray.join(',');
You can put whatever you want as a separator in .join(), like a comma and a space, hyphens, or even words.
1 Comment
In node.js it's usually done with buffers:
> new Buffer([97,112,112,46,106,115,10,110,111,100,101,46,106,115,10]).toString()
'app.js\nnode.js\n'
It'll be faster than fromCharCode, and what's most important, it'll preserve utf-8 sequences correctly.
1 Comment
new Buffer is deprecated, though, and that you should use Buffer.from(array).toString().just use the toString() function:
var yourArray = [97,112,112,46,106,115,10,110,111,100,101,46,106,115,10];
var strng = yourArray.toString();
3 Comments
97,112,112,46,106,115,10,110,111,100,101,46,106,115,10. It looks like OP wants the ASCII values of those numbers, not the list.The ssh2 module passes a Buffer (not an actual javascript array) to 'data' event handlers for streams you get from exec() or shell(). Unless of course you called setEncoding() on the stream, in which case you'd get a string with the encoding you specified.
If you want the string representation instead of the raw binary data, then call chunk.toString() for example.