Solved

C# Sending HEX over TCP

Posted on 2016-08-23
5
55 Views
Last Modified: 2016-10-09
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

0
Comment
Question by:quest_capital
  • 2
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41767796
Try it like this:
postData = "\\x00\\x81SV.97000000009\\x1C123456";

Or like this
postData = @"\x00\x81SV.97000000009\x1C123456";
0
 
LVL 16

Expert Comment

by:DansDadUK
ID: 41768505
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
 
LVL 16

Expert Comment

by:DansDadUK
ID: 41835739
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41836248
angelIII That should have been to split the points with DansDadUK not all to me. Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 48
Regular expression help 2 25
Help with preventing downloading a zip file 10 37
What Does This C# Code Block Do? 5 33
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question