# TCP outputs a string instead of byte values

**URL:** <https://discourse.nodered.org/t/tcp-outputs-a-string-instead-of-byte-values/7201>\
**Category:** General\
**Created:** [24 January 2019 09:23 UTC](https://discourse.nodered.org/t/tcp-outputs-a-string-instead-of-byte-values/7201 "2019-01-24T09:23:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)\
**Post date:** [24 January 2019 09:23 UTC](https://discourse.nodered.org/t/tcp-outputs-a-string-instead-of-byte-values/7201/1 "2019-01-24T09:23:28Z")

</div>

Hi, i wrote a function, it outputs an object message with an array in payload and bytes. I need those bytes to been sent into tcp packet. Actually it converts the buffer to a string and also add commas! how can I overcome it?  
Here is the last parto fo the code:  
var buffer = new Uint8Array (12);  
buffer= [0x55, 0x33, 0x61, 0x39, 0x02, zona, comando1, comando2, comando3, checksum, 0xaa, 0xaa];  
msg={payload:buffer};  
return msg;

Here is the debug log in Node red, it sent correct to TCP:  
24/1/2019, 12:06:41[node: cfc34e3f.3c648](http://192.168.1.251:1880/#)msg : Object

object

payload: array[12]

[0 … 9]

0: 85

1: 51

2: 97

3: 57

4: 2

5: 0

6: 2

7: 16

8: 146

9: 164

[10 … 11]

10: 170

11: 170

\_msgid: "2c01d1a2.dc9b5e"

here is what I sniff receiving the TCP packet:  
\<- {192.168.1.251/37272} 38 35 2C 35 31 2C 39 37 2C 35 37 2C 32 2C 30 2C 32 2C 31 36 2C 31 34 36 2C 31 36 34 2C 31 37 30 2C 31 37 30  
which translated to ascii is:  
85,51,97,57,2,0,2,16,146,164,170,170

it's the buffer, converted in decimal, then string, and added commas!  
I need (hex) {192.168.1.251/37272} 55 33 61 39 02 etc.etc  
thank you!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 January 2019 10:45 UTC](https://discourse.nodered.org/t/tcp-outputs-a-string-instead-of-byte-values/7201/2 "2019-01-24T10:45:33Z")

</div>

try this for your function instead

```
var buffer = Buffer.from( [0x55, 0x33, 0x61, 0x39, 0x02, zona, comando1, comando2, comando3, checksum, 0xaa, 0xaa] );
return {payload:buffer};

```

assuming zona and other variables are set of course

---

<div class="post-metadata">

**Author:** ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)\
**Post date:** [24 January 2019 13:16 UTC](https://discourse.nodered.org/t/tcp-outputs-a-string-instead-of-byte-values/7201/3 "2019-01-24T13:16:35Z")

</div>

thank you works perfect,  
i used the same to produce a simple string followed by "carrier return". Maybe for this there can be a simpler way?

i need to send PWON, i need this payload:  
msg : Object

object

payload: buffer[6][raw](http://192.168.1.251:1880/#)

0: 0x50

1: 0x57

2: 0x4f

3: 0x4e

4: 0xa

5: 0xd  
  
now i used this function, thanks to dceejay:  
var comando = "PWON"  
var buffer = new Uint8Array (comando.length+2);  
for (var i=0; i\<comando.length; i++) {  
buffer[i]=comando.charCodeAt(i);buffer[i+1]=0x0a;buffer[i+2]=0x0d;}  
buffer2= Buffer.from(buffer);  
return {payload:buffer2};

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 January 2019 17:53 UTC](https://discourse.nodered.org/t/tcp-outputs-a-string-instead-of-byte-values/7201/4 "2019-01-24T17:53:53Z")

</div>

```
 var comando = "PWON";
 return {payload: Buffer.from( comando + "\r\n" ) };
```
