C#将十六进制字符串作为十六进制发送

C#将十六进制字符串作为十六进制发送,c#,character-encoding,hex,C#,Character Encoding,Hex,我有一个字符串,我想把它作为十六进制值而不是ascii值发送 我的字符串是: 0x5C 0x21 0x5C 但当我发送时,我发送: 30-78-35-43-20-30-78-32-31-20-30-78-35-43 我怎么能过来 我错过了什么 这是完整的代码: byte[] address = { 0x30, 0x35 }; byte[] end = { 0x03 }; string Start = "0x5C 0x21 0x5C 0x73

我有一个字符串,我想把它作为十六进制值而不是ascii值发送

我的字符串是:

0x5C 0x21 0x5C

但当我发送时,我发送:

30-78-35-43-20-30-78-32-31-20-30-78-35-43

我怎么能过来

我错过了什么

这是完整的代码:

         byte[] address = { 0x30, 0x35 };
          byte[] end = { 0x03 };

        string Start = "0x5C 0x21 0x5C 0x73 0x20 0x73";
        for (int x = 0; x < msg.Count; x++)

        Console.WriteLine(Start);



        byte[] ByteMessage = encoding.GetBytes(Start);

        string HexMessage = BitConverter.ToString(ByteMessage);

        var temp = new MemoryStream();
        temp.Write(address, 0, address.Length);
        temp.Write(ByteMessage, 0, ByteMessage.Length);
        temp.Write(end, 0, end.Length);

        byte[] testing = temp.ToArray();
        var lrs = gen(testing);
        string lrsString = lrs.ToString("X");



        Console.WriteLine("MSG in HEX -  " + HexMessage);

        Console.Write(
                      Encoding.Default.GetString(address) +

                      encoding.GetString(ByteMessage) +

                      Encoding.Default.GetString(end) +

                      );



        byte[] LRS = Encoding.Default.GetBytes(lrsString);

        try
        {

            sp1.Write(start, 0, start.Length); //send start 
            sp1.Write(testing, 0, testing.Length);//send the all msg 
            sp1.Write(LRS, 0, LRS.Length);//send the Check Sum 
        }

        catch (Exception e)
        {
            Console.WriteLine(e);

        }

        finally
        {

        }
byte[]地址={0x30,0x35};
字节[]结束={0x03};
字符串Start=“0x5C 0x21 0x5C 0x73 0x20 0x73”;
对于(int x=0;x

谢谢,

您可能正在寻找解析(每个
字符串
项,如
“0x5C”
应解析为相应的
字节
0x5C
):


您可能正在寻找解析(像
“0x5C”
这样的每个
字符串
项都应该解析为相应的
字节
0x5C
):


您再次将十六进制字符串编码为十六进制。如何获得
string Start=“0x5C 0x21 0x5C”首先?我需要这样做,以便可以使用字节[]发送,否?看看我的帖子——我添加了更多,谢谢,你为什么不使用
byte[]ByteMessage=newbyte[]{0x5C,0x21,0x5C,0x73,0x20,0x73}首先?这是我需要使用的字符串示例。您将十六进制字符串再次编码为十六进制。如何获得
string Start=“0x5C 0x21 0x5C”首先?我需要这样做,以便可以使用字节[]发送,否?看看我的帖子——我添加了更多,谢谢,你为什么不使用
byte[]ByteMessage=newbyte[]{0x5C,0x21,0x5C,0x73,0x20,0x73}首先?这是我需要使用的字符串的示例-如果我的字符串没有“0x”,它会更简单吗?因为我已将“0x”添加到字符串中…@David12123:
Convert.ToByte(第16项)
足够聪明,可以处理或不处理
0x
前缀;这就是为什么
string Start=“5C 21 5C”将被正确解析。您不必添加
“0x”
太好了,因此,我将使用不带“0x”的字符串=非常感谢您的帮助!顺便问一下,如果我的字符串没有“0x”,它会更简单吗?因为我已将“0x”添加到字符串中…@David12123:
Convert.ToByte(第16项)
足够聪明,可以处理或不处理
0x
前缀;这就是为什么
string Start=“5C 21 5C”将被正确解析。您不必添加
“0x”
太好了,因此,我将使用不带“0x”的字符串=非常感谢您的帮助!
string Start = "0x5C 0x21 0x5C";

byte[] ByteMessage = Start
  .Split(' ')                               // Split string into items
  .Select(item => Convert.ToByte(item, 16)) // Parse items into corresponding bytes
  .ToArray();                               // Materialize into array

// Back to Hex (let's have a look on what we are going to send): "5C-21-5C"
string HexMessage = string.Join("-", ByteMessage
  .Select(item => item.ToString("X2")));