4

I have converted a string into their ascii using string.charCodeAt(), but now that I have completed adding/subtracting the values I want to convert them from ASCII back to letters and a string.

I am looking to convert the following array back into their char letters and eventually a string using JavaScript.

 asciiKeys= [70, 69, 69, 69, 32, 67, 66, 68, 69, 32, 67, 65, 77, 67];

I tried using the following, but it keeps stating that it is not a function:

for (var j=0;j<str.length;j++){
    newAsciikeys.push(asciiKeys[j].fromCharCode(0));
}
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Roger Perez
  • 2,807
  • 29
  • 31
  • `String.fromCharCode.apply(String, asciiKeys)` – guest271314 Oct 16 '17 at 00:56
  • What do you think the `0` is doing in `.fromCharCode(0)`? That in itself looks wrong, even aside from the fact that numbers don't have a `.fromCharCode()` method. – nnnnnn Oct 16 '17 at 01:53

3 Answers3

10

fromCharCode is a static function on String. So, this will do what you need, without the need for the loop:

reconstituted = String.fromCharCode.apply(null, asciiKeys);

The apply function is how one sends an array of items to a function as if you had typed in each argument manually. e.g., String.fromCharCode( asciiKeys[0], asciiKeys[1], asciiKeys[2], asciiKeys[3], ... )

(Note that I'm assuming you don't need the intermediate array of characters, and this solution goes straight to the final string you request. If you yet want the intermediate array of characters, you can split the resulting array with reconstituted.split('').)

EDIT: (with thanks to @Kaiido)

For robustness sake, be aware that .apply has a JS engine-specific limit to the number of arguments (read: array size) it can handle. To handle those situations, consider splitting up your work, or falling back to the trusty old loop with one-by-one processing.

hunteke
  • 3,648
  • 1
  • 7
  • 17
  • Beware, on large sequences, the loop might be needed because of [max number of arguments](https://stackoverflow.com/questions/22747068/is-there-a-max-number-of-arguments-javascript-functions-can-accept) `apply` can accept. – Kaiido Oct 16 '17 at 01:13
  • @Kaiido Good point! However, I won't amend the answer, but rather leave that to the implementer to discover (and/or read your comment), as situational context is key. – hunteke Oct 16 '17 at 01:16
  • Fair enough, it's just quite common when dealing with binary manipulation, that we get an hard to debug `RangeError` raising when using this shorthand. – Kaiido Oct 16 '17 at 01:18
  • @GauchoRoger if it worked, then perhaps accept the answer that best responded to your question? – hunteke Oct 16 '17 at 01:28
  • @hunteke I am too much of a noob on stackOverFlow to know how to do that. I clicked the ^ box for your response. It says I need 15 reputation points for me to be able to click the answer worked. – Roger Perez Oct 16 '17 at 02:29
  • @GauchoRoger well, that would be a similar story to myself. I signed up last week, and apparently didn't know that either, else I'd've checked that detail. Thanks, mate. – hunteke Oct 16 '17 at 02:31
  • 1
    @GauchoRoger you can click on the check mark next to an answer to accept it https://stackoverflow.com/tour – Slai Oct 16 '17 at 02:40
  • @Kaiido, do you happen to know what's the limit of arguments length – Bekim Bacaj Oct 16 '17 at 02:47
  • @BekimBacaj I provided a link to an answer which does include these numbers, it's a bit old, but I don't think it has changed drastically either. – Kaiido Oct 16 '17 at 02:50
  • @hunteke. Got it. – Roger Perez Oct 16 '17 at 03:01
  • @GauchoRoger Thanks! (And earned yourself a badge in the process, I see!) :-) – hunteke Oct 16 '17 at 03:04
1

The value within the array needs to be passed to .fromCharCode(); .fromCharCode() is not .charCodeAt()

String.fromCharCode.apply(String, asciiKeys)

Alternatively you can use TextDecoder() to convert an ArrayBuffer representation of array to a string. If expected result is an array you can use spread element to convert string to array.

var asciiKeys = [70, 69, 69, 69, 32, 67, 66, 68, 69, 32, 67, 65, 77, 67];

var str = new TextDecoder().decode(Uint8Array.from(asciiKeys));

console.log(str, [...str]);
guest271314
  • 1
  • 15
  • 104
  • 177
0

On modern browsers (not IE) it can be shortened with the Spread syntax :

s = "ABC", j = JSON.stringify

a = [...s].map(s => s.charCodeAt())  // string to array ( [...s] is short for s.slice() )

r = String.fromCharCode(...a)     // array to string ( (...a) is short for .apply(0, a) )

console.log(j(s), j(a), j(r))
Slai
  • 22,144
  • 5
  • 45
  • 53