There is a code:

public static string GetString(byte[] bytesArray) { string result = string.Empty; for (var i = 0; i < bytesArray.Length; i++) { result += string.Format("0x{0:x2}", bytesArray[i]); if (i != bytesArray.Length - 1) { result += ", "; } } return result; } 

How to reverse convert a string to a byte [] array?

  • one
    Never do a += string variable in a loop! - Pavel Mayorov
  • 2
    @PavelMayorov, never say never. What is the problem of this operator, if the task does not have a large string? - ixSci
  • one
    @ixSci never say never :) Nobody knows how big the size of the input data can be. And when to write everything in the right way is very simple - the addition of lines in a loop should not even be considered. - Pavel Mayorov
  • @Pavel Mayorov, and what other way to do? What is bad cycle, if I know that the input is 32 bytes? - neo
  • one
    @Pavel Mayorov, approx. For the future I will remember. However, the problem is not only this. - neo

3 answers 3

You just have to take and do everything in reverse order.

First, cut the string into delimiters:

 var parts = str.Split(new[] {", "}, StringSplitOptions.None); 

Then create an array of bytes and start filling it in a loop:

 var result = new byte[parts.Length]; for (var i=0; i<parts.Length; i++) { 

From each element of the line, bite the first 0x :

  var part = parts[i]; if (!part.StartsWith("0x")) throw new FormatException(); part = part.Substring(2); 

And bring them to the number

  result[i] = byte.Parse(part, NumberStyles.AllowHexSpecifier); 
  • You don’t need to bite off the first 0x yourself if you use the Convert.ToByte method as it is done in my answer. - Gleb
  • Yes, there is such a way. But it was more important for me to show the process of reversing the algorithm itself back than the skills of searching the standard library. - Pavel Mayorov
  • If we already show the algorithm, it is necessary to show it as a whole and without using methods from the "standard libraries". I would be interested to read how StartsWith and Substring work. If not difficult, add them to your answer. - Gleb

The easiest way is to use the available tools in the form of the Encoding class:

 string str = Encoding.Default.GetBytes("qwerty"); 

PS I repent, inattentively read the question. Your task can be solved for example:

 var res = str.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries) .Select(x => Byte.Parse(x.Substring(2))) .ToArray(); 

Here, however, it is worth considering that this only works for strings that are obtained by your method. But if necessary, you can equip with additional checks of the input line

  • I apologize, but did you even read the question? - Pavel Mayorov
  • The author would like to make a hex decode. How does the qwerty encoding help it? :) - Pavel Mayorov February
  • Why did you decide that byte.Parse "swallow" the hexadecimal value with a prefix? - Pavel Mayorov
  • @Pavel Mayorov, that's not swallowed .... Verified. - neo
  • @PavelMayorov and why did you decide that I decided so? - DreamChild

Since the GetString method (byte [] bytesArray) returns a string approximately in the following format "0x31, 0x32, 0x33", use the following method to inversely convert a string to a byte [] array

 public static byte[] GetBytes(string str) { return str.Split(new[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries) .Select(s => Convert.ToByte(s, 16)) .ToArray(); }