?
Solved

C# Sending HEX over TCP

Posted on 2016-08-23
5
Medium Priority
?
208 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
4 Comments
 
LVL 64

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 64

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 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