I'm already really newbie in coding but my problem is how to divide code likt this "7900BD7400BD7500BD76A5FF" to this "79 00 BD 74 00 BD 75 00 BD 76 A5 FF". My main problem was to convert hex into ascii, but any solution which i got convert only "short" expression. Maybe someone can give me some advices? I'll be really gratefull
-
1Your question is unclear to me. So, you have a string "7900BD7400BD7500BD76A5FF" and you want end with a string where spaces are inserted between the two (hex) characters of each byte. Correct? Because i am confused about you talking about "_convert hex into ascii_" yet you seem to ask about (simple) string manipulation. Thus, i am not really sure what you are asking for... – Jan 07 '19 at 19:50
-
What if your string is an odd number of characters? What if contains non-hex characters? – Dour High Arch Jan 07 '19 at 19:56
-
The other way to go would be to convert the original string to a byte array (see the definitive SO Q&A here: https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa), and then reverse the process by putting a space between every octet (see that SO post as well). This will only work with valid hex strings (which, I'm assuming is one of your constraints) – Flydog57 Jan 07 '19 at 20:06
-
BD FF etc are not ASCII. But you already know that you never use ASCII, right ? The question would be: Which character encoding are you using? – Tom Blodget Jan 08 '19 at 00:24
2 Answers
Since you don't seem to have much knowledge in string processing, I'll give an example that does not require you to lern too many things at once:
string input = "7900BD7400BD7500BD76A5FF";
string output = string.Empty;
for(int i=0; i<input.Length; i+=2) // Go in steps of 2
{
output += input[i]; // The first of 2 characters
output += input[i+1]; // The second of 2 characters
output += ' '; // The space
}
Console.WriteLine(output);
Please note that this solution only inserts spaces after every second character. It does not check whether these are all hex codes and whether its length is a multiple of 2. It assumes that whatever code is before generates a valid result. You should ensure that with unit tests.
This approach will not be very efficient for long strings (if you have long text, please learn about StringBuilder).
If you followed this advice for creating the hex data, then it's of course much easier to insert the space right away:
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:X2} ", b); // <-- I inserted a space in the format string
return hex.ToString();
}

- 55,411
- 20
- 125
- 222
-
Is there a reason behind using `string output = "";` instead of `string output = string.Empty;`? Or is it just personal preference? – Jaskier Jan 07 '19 at 20:00
-
This should use a StringBuilder, rather than string concatenation. String concatenation in a loop is usually a pretty bad code smell. It would be worth checking the length of the string, and, if it's an odd number, putting a space after the first character, and then spaces after every two characters after that – Flydog57 Jan 07 '19 at 20:00
-
Now, i don't really know what the OP is asking for, but if that is what the OP is asking for, the result can be achieved much simpler with a regex replace like `Regex.Replace(input, "(..)(?!$)", "$1 ");` ;-) – Jan 07 '19 at 20:01
-
@Flydog57: I chose this simple approach, because the OP doesn't seem to have much experience. The concept of a StringBuilder may be too much for him – Thomas Weller Jan 07 '19 at 20:01
-
1@Symon: `""` is just less typing when I code in LinqPad. I do replace it by string.Empty usually. – Thomas Weller Jan 07 '19 at 20:02
-
@elgonzo: The concept of a regular expression may be too much for the OP if he doesn't even know how to process strings. – Thomas Weller Jan 07 '19 at 20:02
-
@Symon, actually `string.Empty` is the same as `""`. There is no difference between choosing one or the other (https://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string) aside from style preferences... – Jan 07 '19 at 20:07
-
A more general solution to the problem:
static String SeparateBy(
this string str,
string separator,
int groupLength)
{
var buffer = new StringBuilder();
for (var i = 0; i < str.Length; i++)
{
if (i % groupLength == 0)
{
buffer.Append(separator);
}
buffer.Append(str[i]);
}
return buffer.ToString();
}
And you'd call it like: "7900BD7400BD7500BD76A5FF".SeparateBy(" ", 2)
When posible, and if its relatively easy, try to generalize methods so they can be reused more often. Of course if things start to get complicated generalizing can be self defeating… knowing when or when not to generalize is a skill you will acquire little by little.

- 32,319
- 3
- 50
- 90