?
Solved

C# Sending HEX over TCP

Posted on 2016-08-23
5
Medium Priority
?
118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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