How to get the STX character hex 02

I have a device that I am trying to connect to through a socket, and in accordance with the manual I need the "STX character hex 02".

How to do it with C #?

+6
source share
6 answers

You can use Unicode escape code: \u0002

+8
source

Just a comment for GeoffM's answer (I don't have enough points to comment on the correct path).

You should not insert STX (or other characters) this way using only two digits.

If the next character (after "\ x02") was a valid hexadecimal digit, this would also be parsed, and that would be a mess.

 string s1 = "\x02End"; string s2 = "\x02" + "End"; string s3 = "\x0002End"; 

Here s1 is equal to ".nd", since 2E is a dot symbol, while s2 and s3 are equal to STX + "End".

+12
source

Pass the value of Integer 2 to char :

 char cChar = (char)2; 
+2
source

\x02 - STX code, you can check the ASCII table

 checkFinal = checkFinal.Replace("\x02", "End").ToString().Trim(); 
+1
source

You can embed STX in a string like this:

 byte[] myBytes = System.Text.Encoding.ASCII.GetBytes("\x02Hello, world!"); socket.Send(myBytes); 
0
source

In the line, obviously, the Unicode format is best , but for use as a byte, this approach works:

 byte chrSTX = 0x02; // Start of Text byte chrETX = 0x03; // End of Text // etc... 
0
source

Source: https://habr.com/ru/post/910086/


All Articles