4

I'm getting some strange behaviour when using the parseInt().

webSocket.onmessage = function (event) {
    var raw = event.data;
    alert(raw);
    var data = raw.split(":");

    alert("lat:\"" + data[0] + "\" lon:\"" + data[1] + "\"");

    var x = parseInt(data[0]);
    var y = parseInt(data[1]);

    alert("lat:" + x + " lon:" + y);
}

The first alert outputs: 100:100 - this is the string sent from the server.

The second alert outputs: lat:"100" lon:"100" - which is fine

However, the third alert outputs: lat:1 lon:NaN

What could cause this?

UPDATE:

The problem was the encoding on the server side generating some invisible unwanted characters. I updated the server code and the problem was gone. Working solution.

Community
  • 1
  • 1

2 Answers2

0

My guess is that, there are non-printable characters like spaces, tabs, etc. in your data that's why you're getting NaN after split.

You can use regex to get the data as follow. Using this way, you don't have to worry about non-printable characters as only digits are selected by regex.

var raw = '100:100 ';

var data = raw.match(/\d+/g);

var x = parseInt(data[0], 10);
var y = parseInt(data[1], 10);

document.write('x=' + x + '   y=' + y);

The above regex will select all the digits from raw string.

Tushar
  • 85,780
  • 21
  • 159
  • 179
  • This gives me an array of single digits. data[0] = 1 data[1] = 0 data[2] = 0 data[3] = 1 data[4] = 0 data[5] = 0 – Even A. Rognlien Aug 13 '15 at 10:37
  • @user2667737 Check the Demo in the answer. Please add the data that you're getting in `raw` in the questin – Tushar Aug 13 '15 at 10:42
  • That seems to work, but not with `event.data`.. It gives: "x=1 y=0". But I think you have a point, there seems to be invisible characters. `event.data.length` returns 14, while the printed output is only 7 characters.. – Even A. Rognlien Aug 13 '15 at 11:14
  • 14 chars? Than you are to trim the `raw` variable: `raw.trim()`. Can you provide the full url where you get the 2 data from? – Mr.Web Aug 13 '15 at 11:47
  • The server is currently running local. I suspect something is wrong on that side. Here's the code used for sending the string from the server (written in c#): http://pastebin.com/Ti48xTrJ Based on the code in this post: http://stackoverflow.com/a/12471677/2667737 – Even A. Rognlien Aug 13 '15 at 12:07
0

I agree with @Tushar. Thus, belive some special characters are being parsed in the raw = even.data; (can't replicate here, although)

The code below seems to work fine, you might adjust to yours and test it:

<script type="text/javascript">
    var raw = '100 :100asd';
    var data = raw.match(/[0-9]+/gm);

    alert(raw); // console.log(raw);
    var data = raw.split(":");

    alert("lat:\"" + data[0] + "\" lon:\"" + data[1] + "\"");

    var x = parseInt(data[0]);
    var y = parseInt(data[1]);

    alert("lat:" + x + " lon:" + y);
</script>
lockedz
  • 219
  • 6
  • 15
  • 1
    Thanks for trying to help, but I cannot get that working with `raw = event.data`.. I now tried to access the webpage from a computer, and `alert(event.data)` gave me "1 0 0 : 1 0 0 " (For some reason the Android browser does not output the "spaces", nor does `document.write`). That explains why the length is 14 - there is something between each digit. I tried to replace all spaces with `raw.replace(/ /g, '');`, but the spaces are still there meaning that they probably are something else. – Even A. Rognlien Aug 13 '15 at 12:02
  • @user2667737 I see. You could still try this replace as `raw.replace(/[^\w\s]/gi, '')` Since I'm pretty sure thera are some unicode in between the response :) And just to clarify: the caret (^) is the negation of the set [...], gi say global and case-insensitive and the safelist in this example is digits, word characters, underscores (\w) and whitespace (\s). – lockedz Aug 13 '15 at 12:30