2017-11-07 6 views
0

Ich kämpfe hier ein bisschen. Ich sende eine TCP-Anfrage an einen Endpunkt.C# Konvertieren zu ?? von Wireshark Response

client = new TcpClient(serverIP, serverPort); 
      tcpStream = client.GetStream(); 

      var stringtosend = @"43 50 43 52 00 01 19 00 00 00 1a 00 00 00 
      01 00 00 00 00 00 00 00 32 00 00 01"; 

      stringtosend = stringtosend.Replace(" ", ""); 
      var requestbytes = ConvertToByteArray(stringtosend); 

      tcpStream.Write(requestbytes, 0, requestbytes.Length); 

      List<int> ResponseBytes = new List<int>(); 
      var sb = new StringBuilder(); 

      do 
      { 
       var byteRead = tcpStream.ReadByte(); 
       if (byteRead != -1) 
       { 
        sb.Append(byteRead); 
       } 
       else 
       { 
        break; 
       } 

      } while (tcpStream.DataAvailable); 

Dies ist die Antwort als von wireshark lesen:

43 50 43 52 00 01 19 00 00 00 34 01 00 00 1 B 01 00 00 0B 01 0B 01 3C 00 00 32 00 01 FF FF 0C 00 48 56 41 43 20 43 54 52 4C 00 02 02 40 01 46 0B 01 00 00 00 00 00 00 45 4D 2D 4D 38 38 49 23 31 00 11 00 00 00 00 81 01 00 00 00 00 00 00 00 41 43 31 4D 38 38 49 23 32 00 11 00 00 00 00 81 02 00 00 00 00 00 00 45 4D 2D 4D 38 38 4F 23 31 00 12 00 00 00 00 82 01 00 00 00 00 00 00 41 43 31 4D 38 4F 23 32 00 12 00 00 00 00 82 02 00 00 00 00 00 00 41 43 32 4D 52 54 55 23 31 00 0F 03 0B 01 46 90 01 00 00 00 00 00 00 41 43 33 4D 52 54 55 23 32 00 0F 03 0B 01 46 90 02 00 00 00 00 00 00 41 43 34 4D 52 54 55 23 33 00 0F 03 0B 01 46 90 03 00 00 00 00 00 00 41 43 35 4D 52 54 55 23 34 00 0F 03 0B 01 46 90 04 00 00 00 00 00 00 41 43 36 4D 52 54 55 23 35 00 0F 03 0B 01 46 90 05 00 00 00 00 00 00 52 41 43 4B 20 31 26 32 00 00 03 02 40 01 46 01 01 00 00 00 00 00 00 52 41 43 4B 20 33 26 34 00 00 03 02 40 01 46 02 01 00 00 00 00 00 00 CPCR ...... 4 ............ < ..2..ÿÿ..HVAC CTRL ... @. F ........ EM-M88I # 1 ............. AC1M88I # 2 ............. EM-M88O # 1 ............. AC1M88O # 2 ............. AC2MRTU # 1 ..... F ....... AC3MRTU # 2 ..... F ....... AC4MRTU # 3 ..... F ....... AC5MRTU # 4 ..... F ....... AC6MRTU # 5 ..... F ....... RACK 1 & 2. ... @. F ........ RACK 3 & 4 .... @. F ........

Ich bin interessiert, wie zu konvertieren, was ich glaube zu sein eine Hex-Antwort in was ist fett d oben.

Ich bin neu in diesem so bitte sei sanft.

+0

Jedes Sechseck Paar und Ausgang nehmen die Reaktion Zeichen diese hex Nummer ... was hast du probiert – BugFinder

+0

Wie mache ich das? Ich versuchte utf8 Decodierung, und einige andere von einer Website, die Hexadezimal zu ... – RSH

+0

Nun, wenn Sie hexadezimal verstanden haben, würden Sie nicht schwer zu verstehen, was zu tun ist. Also lese einfach Hexadezimalzahlen ein und komm zurück und stelle eine Frage, wenn du Probleme hast oder versucht hast, sie zu lösen, weil es für mich so klingt, als würdest du nur versuchen, uns dazu zu bringen, deine Arbeit zu machen.Ich gebe dir einen Hinweis, jedes Paar repräsentiert einen Charakter in deinem Fall. Bsp .: '43' wäre' C', '50' wäre' P' usw. Sie können sich je nach Kodierung unterscheiden, also können Sie die Werte in ein Byte-Array übersetzen und die eingebaute Funktionalität von 'System verwenden .Text.Encoding' etc. – Bauss

Antwort

0

Am einfachsten Sie folgendes tun können, die sich direkt aus

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types

// Convert the number expressed in base-16 to an integer. 
    int value = Convert.ToInt32(hex, 16); 
    // Get the character corresponding to the integral value. 
    string stringValue = Char.ConvertFromUtf32(value); 

Auch ich habe wie die folgende

http://www.rapidtables.com/convert/number/hex-to-ascii.htm 
0

genommen wurde ich dies gerade geklopft habe verwendet, um Websites als eine Konsolenanwendung, so dass Sie in der Lage sein sollten, kopieren und einfügen als Ihre Program.cs:

using System; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      const string inputString = "43 50 43 52 00 01 19 00 00 00 34 01 00 00 1B 01 00 00 0B 01 0B 01 3C 00 00 32 00 01 FF FF 0C 00 48 56 41 43 20 43 54 52 4C 00 02 02 40 01 46 0B 01 00 00 00 00 00 00 45 4D 2D 4D 38 38 49 23 31 00 11 00 00 00 00 81 01 00 00 00 00 00 00 41 43 31 4D 38 38 49 23 32 00 11 00 00 00 00 81 02 00 00 00 00 00 00 45 4D 2D 4D 38 38 4F 23 31 00 12 00 00 00 00 82 01 00 00 00 00 00 00 41 43 31 4D 38 38 4F 23 32 00 12 00 00 00 00 82 02 00 00 00 00 00 00 41 43 32 4D 52 54 55 23 31 00 0F 03 0B 01 46 90 01 00 00 00 00 00 00 41 43 33 4D 52 54 55 23 32 00 0F 03 0B 01 46 90 02 00 00 00 00 00 00 41 43 34 4D 52 54 55 23 33 00 0F 03 0B 01 46 90 03 00 00 00 00 00 00 41 43 35 4D 52 54 55 23 34 00 0F 03 0B 01 46 90 04 00 00 00 00 00 00 41 43 36 4D 52 54 55 23 35 00 0F 03 0B 01 46 90 05 00 00 00 00 00 00 52 41 43 4B 20 31 26 32 00 00 03 02 40 01 46 01 01 00 00 00 00 00 00 52 41 43 4B 20 33 26 34 00 00 03 02 40 01 46 02 01 00 00 00 00 00 00"; 

      var bytes = HexStringToBytes(inputString); 

      var readableString = Encoding.UTF7.GetString(bytes).ToCharArray(); 

      var paddedReadableString = readableString.Select(ch => char.IsControl(ch) ? '.' : ch).ToArray(); 

      var outputString = new String(paddedReadableString); 
      Console.WriteLine(outputString); 
      Console.ReadLine(); 
     } 

     private static byte[] HexStringToBytes(string hex) 
     { 
      var strippedHex = Regex.Replace(hex, @"\s+", ""); 

      byte[] raw = new byte[strippedHex.Length/2]; 
      for (int i = 0; i < raw.Length; i++) 
      { 
       raw[i] = Convert.ToByte(strippedHex.Substring(i * 2, 2), 16); 
      } 
      return raw; 
     } 
    } 
} 

Für Informationen ist Ihr gewünschtes Format UTF7.

0

Sie lesen Ihre Bytes direkt in einen StringBuilder.

Der Schlüssel Linie ist hier:

var byteRead = tcpStream.ReadByte(); 

Sie die Möglichkeit haben, die Codierung hier mit this static method zu konvertieren, die wie etwas aussehen:

// Create two different encodings. 
    Encoding utf8 = Encoding.UTF8; 
    Encoding unicode = Encoding.Unicode; 

    // Perform the conversion from one encoding to the other. 
    byte[] unicodeBytes = Encoding.Convert(utf8, unicode, new byte[]{byteRead}); 
+0

Danke euch allen! Es funktioniert jetzt perfekt. – RSH