0

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]
Morgan Estes
  • 223
  • 3
  • 16
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111

5 Answers5

7
var result = String.fromCharCode.apply(null, arrayOfValues);

JSFiddle

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.

Bali Balo
  • 3,338
  • 1
  • 18
  • 35
4

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.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 1
    String.fromCharCode.apply(0, [97,112,112,46,106,115,10,110,111,100,101,46,106,115,10]) avoids un-needed looping and vars, is less to type, and faster to execute. ed: looks like Bali Jinxed me... – dandavis Feb 20 '14 at 19:57
3

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.

alex
  • 11,935
  • 3
  • 30
  • 42
  • 2
    Yes, definitely the right way to go in Node. Note that `new Buffer` is deprecated, though, and that you should use `Buffer.from(array).toString()`. – Mark Birbeck Apr 27 '18 at 16:44
1

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();

Kuri
  • 109
  • 1
  • 8
  • Yeah, it worked for me. Actually it is in w3schools: http://www.w3schools.com/jsref/jsref_tostring_array.asp – Kuri Feb 20 '14 at 20:00
  • I just tried it again; something was wrong with my console, apparently. It "works," but merely produces the text `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. – elixenide Feb 20 '14 at 20:02
  • 4
    Also, w3schools is *not* a good resource. Despite its name, it is in no way official, authoritative, or associated with the W3C. In fact, it's notorious for spreading incorrect and obsolete information. – elixenide Feb 20 '14 at 20:03
0

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.

mscdex
  • 104,356
  • 15
  • 192
  • 153