troubleshooting Question

C# TCP SEND ASCII CHARACTERS

Avatar of ratitamaldita
ratitamaldita asked on
Editors IDEsC#Networking Protocols
8 Comments1 Solution2304 ViewsLast Modified:
Windows service c#

Hi gurus, i have the next problem, my code need send a tcp string, the first 2 bytes of the string are the lenght of the string.


Example:

I have a string of length 690 = 02 b0 in hexadecimal, but the receptor receives 02 C2 B2, whereas it should receive 02 B2.

I have proven in UTF8 encoding and ASCII, but I'm not getting the desired result

Please help me to fix this issue

Thanks for all solutions
// Get the string lenght 
char[] lk = ToCharsBitShift(sXMLResponse.Length);
 
// attach the lenght to the string
sXMLResponse = lk[0].ToString() + lk[1].ToString() + sXMLResponse;
 
//Encode the string to bytes
Byte[] ClientResponse = Encoding.UTF8.GetBytes(sXMLResponse);
 
//Send the response to client
clientStream.Write(ClientResponse, 0, ClientResponse.Length);
clientStream.Flush()
 
 
 public static char[] ToCharsBitShift(int x)
 {
      char[] chars = new char[2];
 
      chars[1] = (char)(x & 0xFF);
      chars[0] = (char)(x >> 8  & 0xFF);
      
      return chars;
 }
ASKER CERTIFIED SOLUTION
philipjonathan

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros