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?

4
  • 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()); Commented Nov 21, 2013 at 19:45
  • String.fromCharCode.apply(String,arr); Commented Nov 21, 2013 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. Commented Nov 21, 2013 at 19:48
  • 4
    Are the bytes (which look like strings, actually) supposed to be character codes? If so, what's the encoding? Commented Nov 21, 2013 at 19:48

3 Answers 3

7

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,'');
Sign up to request clarification or add additional context in comments.

8 Comments

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.
@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 :-)
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.
it is UTF-8. I did need to include that in OP.
"actually supports an infinite number of parameters" are you sure about this?
|
4

Here's one more alternative using map:

var str = arr.map(String.fromCharCode).join("");

Comments

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

4 Comments

Do you really want the zero character at the end of the string?
@TedHopp: It just returns a blank string, so it doesn't do anything harmful.
@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.
@TedHopp: Ah! Yes, you are right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.