# TCP In/TCP out and char "§"

**URL:** <https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582>\
**Category:** Core Development\
**Created:** [1 October 2018 07:43 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582 "2018-10-01T07:43:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![node-guy](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@node-guy](https://discourse.nodered.org/u/node-guy)\
**Post date:** [1 October 2018 07:43 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/1 "2018-10-01T07:43:22Z")

</div>

Good morning together,

I have a question about TCP IN/OUT node. As my Node-Red program receives the "§" character it's output as debug on console is a not printable character. I use the String.charCodeAt(...) function and it prints out 65533.  
Why?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [1 October 2018 07:56 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/2 "2018-10-01T07:56:25Z")

</div>

Javascript uses 16 bits to represent characters in strings

---

<div class="post-metadata">

**Author:** ![node-guy](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@node-guy](https://discourse.nodered.org/u/node-guy)\
**Post date:** [1 October 2018 08:02 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/3 "2018-10-01T08:02:22Z")

</div>

Ok. But var res = "§".charCodeAt(0); in JavaScripts always returns 167. Node-Red returns 65533...

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [1 October 2018 08:59 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/4 "2018-10-01T08:59:37Z")

</div>

> [@node-guy](#):
>
> charCodeAt

Well, I'm no JS expert , but

> **[String.prototype.charCodeAt() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt)**
>
> The charCodeAt() method of String values returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

implies that it returns 16bits

Maybe the JS interpreter (is this in a browser?) your using for comparison does some extra stuff behind the scenes?

Hopefully a JS expert will be along to explain the discrepancy 🙂  
Simon

---

<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:** [1 October 2018 12:12 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/5 "2018-10-01T12:12:01Z")

</div>

as the tcp node returns a buffer - why not just look at the character value directly ? why are you trying to make it into a string then back ?

---

<div class="post-metadata">

**Author:** ![node-guy](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@node-guy](https://discourse.nodered.org/u/node-guy)\
**Post date:** [1 October 2018 12:46 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/6 "2018-10-01T12:46:25Z")

</div>

I can't look direct to the character. My Node-red flow needs to read out a string from a network port 10000 and needs to prepare it for some reason. When a string conatins "a(bc?§d" characters for example, how can I react on a "§" character...

---

<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:** [1 October 2018 18:20 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/7 "2018-10-01T18:20:00Z")

</div>

You can use Buffer.indexOf to find the location of that character in a buffer  
[https://nodejs.org/api/buffer.html#buffer\_buf\_includes\_value\_byteoffset\_encoding](https://nodejs.org/api/buffer.html#buffer_buf_includes_value_byteoffset_encoding)

Or you could convert to a string using a different encoding... maybe binary  
[https://nodejs.org/api/buffer.html#buffer\_buffers\_and\_character\_encodings](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings)  
as that would handle non-printable characters better.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [1 October 2018 19:47 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/8 "2018-10-01T19:47:53Z")

</div>

> [@node-guy](#):
>
> As my Node-Red program receives the "§" character it's output as debug on console is a not printable character.

Looks like you are getting the [Unicode Replacement character](https://en.wikipedia.org/wiki/Specials_(Unicode_block)) passed in through your tcp connection -- why? only you can figure that out, i suspect... but there are some clues on that Wikipedia page:

> The **replacement character** � (often a black diamond with a white question mark or an empty square box) is a symbol found in the [Unicode](https://en.wikipedia.org/wiki/Unicode) standard at code point U+FFFD in the _Specials_ table. It is used to indicate problems when a system is unable to render a stream of data to a correct symbol. It is usually seen when the data is invalid and does not match any character

So it probably depends upon what the raw data is in the sending code, what language and "encodings" were used to serialize the data onto the socket, AND what language and encodings were used at the receiving side (node-red). However, it seems safe to say that at least 1 of those components is not using the correct encoding.

If you have access to the source code that is sending the data, and/or the raw data itself, that would be a good place to start debugging.

---

<div class="post-metadata">

**Author:** ![node-guy](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@node-guy](https://discourse.nodered.org/u/node-guy)\
**Post date:** [2 October 2018 06:42 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/9 "2018-10-02T06:42:50Z")

</div>

Hi,

thanks to all for your answer. That's helps me. Seems that's really comes as unicode.

---

<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:** [2 October 2018 07:53 UTC](https://discourse.nodered.org/t/tcp-in-tcp-out-and-char/3582/10 "2018-10-02T07:53:12Z")

</div>

well it may well not be unicode on the wire / in the buffer... but as soon as you turn it into a string (or print it) it will be... hence my repeated asking you to go look at the raw buffer.
