Solved

C# Sending HEX over TCP

Posted on 2016-08-23
5
65 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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