-1

I need to convert a byte array into a readable plain text string. I had a look at this How do you convert a byte array to a hexadecimal string, and vice versa? but it converts it to a hex string in ASCII. I am looking for a plain text string equivalent.

i.e when you make use of this website https://codebeautify.org/hex-string-converter , you should be able to post your hexidecimal text and get the converted string.

Take this Hex string for example: 47617465776179536572766572, the plain text output is GatewayServer I want to achieve this in code.. my packet data is coming as a byte array.

Harold_Finch
  • 682
  • 2
  • 12
  • 33

2 Answers2

1

Simple Linq seems to be enough: we extract ascii codes: 47, 61, 74, ... 72 convert them into chars and finally Concat all these characters into string:

Code:

  using System.Linq;
  using System.Globalization;
  ...

  string source = "47617465776179536572766572";

  string result = string.Concat(Enumerable
    .Range(0, source.Length / 2)
    .Select(i => (char)int.Parse(source.Substring(2 * i, 2), NumberStyles.HexNumber)));

  // Let's have a look
  Console.Write(result);

Outcome:

  GatewayServer
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You can do something like this

var encodedString = System.Text.Encoding.Default.GetString(byteArray);

sINFflwies
  • 69
  • 2
  • 6