1

I am getting a response from a SOAP server and it is an array of bytes, fixed size.

For instance, I get { rsp: { errorNumber: '1', errorMessage: { item: [Object] } } }

[ '73',
  '110',
  '118',
  '97',
  '108',
  '105',
  '100',
  '32',
  '112',
  '97',
  '115',
  '115',
  '119',
  '111',
  '114',
  '100',
  '0']

How do I turn that array to a string it javascript?

karthikr
  • 97,368
  • 26
  • 197
  • 188
reza
  • 5,972
  • 15
  • 84
  • 126
  • You just want that array to be a string? `console.log([ '73', '110', '118', '97', '108', '105', '100', '32', '112', '97', '115', '115', '119', '111', '114', '100', '0'].toString());` – brbcoding Nov 21 '13 at 19:45
  • `String.fromCharCode.apply(String,arr);` – gen_Eric Nov 21 '13 at 19:47
  • 1
    @JacobM, people aren't asking their questions clearly. OP literally posted an array and asked how to turn it into a string. – brbcoding Nov 21 '13 at 19:48
  • 4
    Are the bytes (which look like strings, actually) supposed to be character codes? If so, what's the encoding? – Ted Hopp Nov 21 '13 at 19:48

3 Answers3

6

Each "byte" in your array is actually the ASCII code for a character. String.fromCharCode will convert each code into a character.

It actually supports an infinite number of parameters, so you can just do:

String.fromCharCode.apply(String, arr);

When ran on your array you get: "Invalid password".

As @Ted Hopp points out, the 0 at the end is going to add a null character to the string. To remove it, just do: .replace(/\0/g,'').

String.fromCharCode.apply(String, arr).replace(/\0/g,'');
Community
  • 1
  • 1
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • How do you know that it's an ASCII encoding? OP didn't post the XML header of the response. I'll grant that it is most likely utf-8 (which agrees with ASCII in the range actually used), in fact it could be almost anything. Not all valid encodings agree with ASCII in the range of codes provided. – Ted Hopp Nov 21 '13 at 19:58
  • @TedHopp: I don't know the encoding, but it just so happened to be ASCII. I just kinda assumed, and when I tested it and got `"Invalid password"`. So, I figured my assumption was ok :-) – gen_Eric Nov 21 '13 at 19:59
  • Yeah, I'm sure it's UTF-8 (or ASCII, or ISO-8859-something); they all agree for the range of codes that OP posted. I'm just saying that you're making an assumption that OP would need to confirm, particularly if some of the other responses aren't in the Basic Latin block. – Ted Hopp Nov 21 '13 at 20:02
  • it is UTF-8. I did need to include that in OP. – reza Nov 21 '13 at 22:33
  • "actually supports an infinite number of parameters" are you sure about this? – David Apr 23 '19 at 14:49
  • @David I'm sure there is a limit to the number of array elements/parameters, but I'd imagine that's not something you'd generally need to worry about. – gen_Eric Apr 23 '19 at 15:19
  • @David The spec shows no limit, but each browser seems to be different: https://stackoverflow.com/a/22747272 – gen_Eric Apr 23 '19 at 15:32
  • @Rocket Hazmat in my case I need to base64 encode an entire file that the user selects, so it could be a 100MB file, thus having performance issues because string + string with such a big array fill up the RAM to over 1.5GB while the loop is going through. I tried using you solution with window.btoa(result) but I get invalid non latin1 found or something like that, I don't get this error when I use a normal for loop with str+= String.fromCharCode(foo[i]); – David Apr 23 '19 at 15:47
4

Here's one more alternative using map:

var str = arr.map(String.fromCharCode).join("");
megawac
  • 10,953
  • 5
  • 40
  • 61
2

Here is what you want, the String.fromCharCode function:

var foo = [ 
  '73',
  '110',
  '118',
  '97',
  '108',
  '105',
  '100',
  '32',
  '112',
  '97',
  '115',
  '115',
  '119',
  '111',
  '114',
  '100',
  '0'];

var str = '';
for (var i=0; i<foo.length; ++i) {
  str+= String.fromCharCode(foo[i]);
}

console.log(str);

Or better :

var str = String.fromCharCode.apply(null, foo);
OlivierH
  • 3,875
  • 1
  • 19
  • 32
  • Do you really want the zero character at the end of the string? – Ted Hopp Nov 21 '13 at 19:49
  • @TedHopp: It just returns a blank string, so it doesn't do anything harmful. – gen_Eric Nov 21 '13 at 19:50
  • 1
    @RocketHazmat - Actually, it returns a string containing '\u0000', not a blank. It can screw up string comparisons and other processing, depending on what's being done. This answer also assumes that the codes are Unicode, when they could be in another encoding entirely. – Ted Hopp Nov 21 '13 at 19:53
  • @TedHopp: Ah! Yes, you are right. – gen_Eric Nov 21 '13 at 19:53