C# 我如何获得十六进制 02 的 STX 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9585828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the STX character of hex 02
提问by David
I have a device to which I'm trying to connect via a socket, and according to the manual, I need the "STX character of hex 02".
我有一个要通过套接字连接的设备,根据手册,我需要“十六进制 02 的 STX 字符”。
How can I do this using C#?
我怎样才能使用 C# 做到这一点?
采纳答案by SLaks
You can use a Unicode character escape: \u0002
您可以使用 Unicode 字符转义: \u0002
回答by Shai
Cast the Integervalue of 2 to a char:
将Integer2的值转换为 a char:
char cChar = (char)2;
回答by GeoffM
You can embed the STX within a string like so:
您可以将 STX 嵌入字符串中,如下所示:
byte[] myBytes = System.Text.Encoding.ASCII.GetBytes("\x02Hello, world!");
socket.Send(myBytes);
回答by Vegard Innerdal
Just a comment to GeoffM's answer (I don't have enough points to comment the proper way).
只是对 GeoffM 的回答发表评论(我没有足够的分数来评论正确的方式)。
You should never embed STX (or other characters) that way using only two digits.
您永远不应该仅使用两位数字以这种方式嵌入 STX(或其他字符)。
If the next character (after "\x02") was a valid hex digit, that would also be parsed and it would be a mess.
如果下一个字符(在“\x02”之后)是一个有效的十六进制数字,那么它也会被解析,这将是一团糟。
string s1 = "\x02End";
string s2 = "\x02" + "End";
string s3 = "\x0002End";
Here, s1 equals ".nd", since 2E is the dot character, while s2 and s3 equal STX + "End".
这里,s1 等于“.nd”,因为 2E 是点字符,而 s2 和 s3 等于 STX +“End”。
回答by Jawad Siddiqui
\x02is STX Code you can check the ASCII Table
\x02是 STX 代码,您可以查看ASCII 表
checkFinal = checkFinal.Replace("\x02", "End").ToString().Trim();
回答by CJBS
Within a string, clearly the Unicode format is best, but for use as a byte, this approach works:
在字符串中,显然Unicode 格式是最好的,但对于用作字节,这种方法有效:
byte chrSTX = 0x02; // Start of Text
byte chrETX = 0x03; // End of Text
// etc...

