1

I am struggling a bit here. I am sending an tcp request to an endpoint.

 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);

This is the response as read from wireshark:

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 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........

I am interested in how to convert what I believe to be a hex response into what is bolded above.

I'm new at this so please be gentle.

RSH
  • 373
  • 1
  • 5
  • 17
  • take each hex pair and output the characters responding to that hex number... what did you try – BugFinder Nov 07 '17 at 13:16
  • How do I do that? I tried utf8 decoding, and several others from a website that converts hex to... – RSH Nov 07 '17 at 13:22
  • Well if you understood hexadecimal you wouldn't have trouble understanding what to do. So simply read up on hexadecimals and come back and ask a question when you're actually having trouble or have tried to solve it, because to me it sounds like you're just trying to get us to do your work. I'll give you a hint, each pair represents a character in your case. Ex: `43` would be `C`, `50` would be `P` etc. It might differ per encoding, so what you could do is translate the values into a byte array and use the build-in functionality from `System.Text.Encoding` etc. – Bauss Nov 07 '17 at 13:25
  • You have to remember that the string response shown is a representation, not actual truth, so its taken 43 as hex and printed character 67 which is C .. but that doesnt necessarily mean thats what 43 was part of.. you could have read [this](https://stackoverflow.com/questions/1139957/c-sharp-convert-integer-to-hex-and-back-again) from SO... oh and dont forget not all characters are really printable.. – BugFinder Nov 07 '17 at 13:27
  • I can only aspire to be as great as you someday. – RSH Nov 07 '17 at 13:35

3 Answers3

0

At the simplest you can do the following which was taken directly from

https://learn.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);

Also I have used websites like the following

http://www.rapidtables.com/convert/number/hex-to-ascii.htm
nate_weldon
  • 2,289
  • 1
  • 26
  • 32
0

I've just knocked this up as a console application, so you should be able to copy and paste as your 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;
        }
    }
}

For info, your desired format is UTF7.

LordWilmore
  • 2,829
  • 2
  • 25
  • 30
0

You are reading your bytes direct into a StringBuilder.

The key line here is:

var byteRead = tcpStream.ReadByte();

You have an opportunity to convert the encoding here using this static method which will look something like:

  // 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} );
Bijan Rafraf
  • 393
  • 1
  • 7