C# Sending HEX over TCP

I am trying to send data via TCP.
Some data as to be sent in HEX format while some data doesn't.
Here is an example string whats in brackets should be in HEX:
[\x00\x81]SV.97000000009[\x1C]123456

Please advise how to send this data properly.

Here is the code:
postData = "\x00\x81SV.97000000009\x1C123456";

                var server = "localhost";
                var port = 5100;

                TcpClient client = new TcpClient(server, port);

                byte[] bytes = Encoding.ASCII.GetBytes(postData);
                
                NetworkStream stream = client.GetStream();
                stream.Write(bytes, 0, bytes.Length);

                bytes = new Byte[256];
                String responseData = String.Empty;

                Int32 data = stream.Read(bytes, 0, bytes.Length);
                responseData = Encoding.ASCII.GetString(bytes, 0, data);

                var result = responseData;
                stream.Close();
                client.Close();                

Open in new window

quest_capitalAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Try it like this:
postData = "\\x00\\x81SV.97000000009\\x1C123456";

Or like this
postData = @"\x00\x81SV.97000000009\x1C123456";
0
 
DansDadUKCommented:
ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F.

So your 0x81 byte is not an ASCII character.
0
 
DansDadUKCommented:
Although I made a pertinent point regarding the definition of ASCII, Fernando Soto provided a practical answer to how to do what the questioner asked.

So perhaps the points should be split?
0
 
Fernando SotoRetiredCommented:
angelIII That should have been to split the points with DansDadUK not all to me. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.