29

I might be asking the wrong question here, so I apologize if the answer is on another thread... but I've looked around to no avail.

in a snippet, why doesn't this work?

array = [72,69,76,76,79];
document.write(String.fromCharCode(array));

I'm collecting key events in an array and want to be able to write them out as chars when prompted. And though this works:

document.write(String.fromCharCode(72,69,76,76,79));

I can't seem to get it to work when I pass it along as an array. I've also tried to convert the array toString() first, as well array.join(","); to create a comma separated list ...yet nothing. Any ideas? Is there a better way to convert the values I collect in my array into chars?

alex
  • 479,566
  • 201
  • 878
  • 984
Nick Briz
  • 1,917
  • 3
  • 20
  • 34

8 Answers8

45

You could use the function's apply() method...

document.write(String.fromCharCode.apply(null, array));

jsFiddle.

ES6 can use the spread operator...

document.write(String.fromCharCode(...array));

You could also use the array's reduce() method, but older IEs do not support it. You could shim it, but the apply() method is better supported.

document.write(array.reduce(function(str, charIndex) {
    return str += String.fromCharCode(charIndex);
}, ''));​

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
  • 6
    Take in the account that this trick is potentially dangerous. In case if you will try to construct very long strings this way (e.g. preparing ArrayBuffer for base64 encoding), you will have a range exception. – domax Oct 26 '13 at 20:18
10

Yes, you can use apply() to call a function which an array passed in as its arguments:

array = [72,69,76,76,79];
document.write(String.fromCharCode.apply(String, array));
Paul
  • 139,544
  • 27
  • 275
  • 264
3

If you use .apply() to call the fromCharCode() function, you can pass it an array that will be converted into arguments for the function like this:

document.write(String.fromCharCode.apply(this, array));

You can see it work here: http://jsfiddle.net/jfriend00/pfLLZ/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

In new version of javascript, you can use spread syntax

const array = [72,69,76,76,79];
document.write(String.fromCharCode(...array));
Kiochan
  • 457
  • 1
  • 7
  • 12
1

The method is more meant to be used like this:

document.write(String.fromCharCode(72,69,76,76,79));

You're passing in an array when the method expects multiple parameters as a list.

alex
  • 479,566
  • 201
  • 878
  • 984
Levi Hackwith
  • 9,232
  • 18
  • 64
  • 115
0

You may have to use a loop as follows

for(var i = 0; i < array.length; i++)
       document.write(String.fromCharCode(array[i]));
}
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
-2

Here's 2 ways that I would do it:

var arr=[119,119,119,46,87,72,65,75,46,99,111,109];
document.write(eval('String.fromCharCode('+arr+')'));

document.write('<hr>');

var arr='119,119,119,46,87,72,65,75,46,99,111,109';
document.write(eval('String.fromCharCode('+arr+')'));
Dave Brown
  • 923
  • 9
  • 6
  • Depending on the implicit conversion of the array to a string is not immediately visible, sadly. – Kissaki Jan 30 '16 at 13:22
-2

Here is a function:

var unCharCode = function(x) {
  return this["eval"]("String['fromCharCode'](" + x + ")");
};

document.write(unCharCode([ 119, 119, 119, 46, 87, 72, 65, 75, 46, 99, 111, 109 ]));

document.write("<hr>");

document.write(unCharCode("119,119,119,46,87,72,65,75,46,99,111,109"));
Dave Brown
  • 923
  • 9
  • 6