1

I have a back end code running in c++. I am writing a front end code in nodejs. On opening a TCP connection with server I get a struct from it which is in this format:

struct Details {
    char short_name_[16];
    char name_[32];
}

I am able to receive it on front end. But I want to split it back to two strings.

I tried to randomly assign short_name_ to "aaaaaaaaaaaaaaa" and similarly name_ with all 'b's. When I print data on front end

console.log('DATA ' + client.remoteAddress + ': ' + data);

output is

 DATA 127.0.0.1: aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

I want to separate them back in two variables. Since char is 1 byte, I tried to convert them to a string object and tried splitting but it did not work as JSON.stringify is converting this to string to like an array with 16 97s and 32 98s. [97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98]

Also how should I do this if there integers(4 bytes) in received structs?

SS306
  • 157
  • 1
  • 3
  • 9

1 Answers1

1

You can use the built-in object Buffer.

If have not tried the following example, but it should work. If not, then you have a start point for your own tries ;-).

var buf = new Buffer(data);
var short_name = buf.toString('ascii', 0, 16);
var name = buf.toString('ascii', 16, 32);
Kuba Holuj
  • 687
  • 1
  • 6
  • 16
Peter Paul Kiefer
  • 2,114
  • 1
  • 11
  • 16