0

Morning guys So I have this sanario a user enters text into a textbox the text is something in the following format

0123456789ABCDEF

I want to be able to split the Text given by the user as

01-23-45-67-89-AB-CD-EF

is this possible ?

And then if possible how can I convert this string to byte ?

All done in c#

Thanks in advance

HeadJ.E.M.
  • 101
  • 8

4 Answers4

1
string output = "0123456789ABCDEF";
int i = 2;

while (i < output.Length) {
    output = output.Insert(i, "-");
    i += 3;
}
0

Could you do it this way for a byte array?

string output = "0123456789ABCDEF";
int i = 2;

while (i < output.Length) {
    output = output.Insert(i, ",");
    i += 3;
}

byte[] array = {output};
0

This may have been answered here: Converting String to Byte Array

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}
Community
  • 1
  • 1
aRBee
  • 166
  • 1
  • 6
0

Here's my preferred way:

var result = String.Join("-", text.Buffer(2).Select(x => new string(x.ToArray())));

You just need to get the Interactive Framework (NuGet Ix-Main) from Microsoft's Reactive Framework team.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172