2

I am trying to send binary data from C++ to Javascript and interpret it byte per byte. For this I use a simple TCP server and the net module from node.js. Here is the C++ Code:

    char a[4]={128,0,0,1};
    std::stringstream stream;
    stream.write(a,sizeof(a));
    server.send(stream);

And for the Javascript side :

var client = new net.Socket();
client.setEncoding('utf8');
client.setNoDelay(true);

client.connect({port:25003},handler);

client.on('data', function(data) {
    var thedata=data;
    console.log('Received from C++: ' + thedata.length+"bytes");
        console.log('DATA: '+thedata);
      for(var i=0;i<thedata.length;i++)
      {
          buffer1[i]=thedata.charCodeAt(i);
      }
      console.log(buffer1[0]);
});

Code explained shortly:

C++ sends 4 bytes over TCP: 128 0 0 1

Javascript saves the received data in an Uint8Array with the charCodeAt function.

So basically my problem is that I can only send "numbers" from 0-127 because in javascript it seems that I need to use the charCodeAt function, which goes crazy for values over 127. And even worse, when I try to send this over a socket.io connection it creates a 16bit String.

How can I handle the bytes on the Javascript side? I mean, simply interpret it back as 128 0 0 1?

4
  • developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/… Commented Aug 1, 2015 at 17:09
  • 1
    @max I think the JavaScript code is running in Node Commented Aug 1, 2015 at 17:09
  • yes it is running in Node Commented Aug 1, 2015 at 17:13
  • 2
    I think that you want to avoid calling .setEncoding() altogether - you don't want Node to interpret the stream of bytes as being characters with any particular encoding. If you don't call that, then your "data" events will be passed Buffer instances instead of strings. Commented Aug 1, 2015 at 17:17

1 Answer 1

2

Because you used this line: client.setEncoding('utf8');

Over 127, the highest bit will be set to 1, meaning the current character is coded over several bytes (which seems not your case as it's binary). You are confusing bytes and characters.

Try other encoding, like ASCII but utf8.

Sign up to request clarification or add additional context in comments.

1 Comment

This is on the right track - I believe that the right thing to do is to avoid setting any encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.