2

So, this is what happened:

I'm working on a project to university, and i have a class in C# with these following arrays.

    private byte[] Key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
    private byte[] Vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 252, 112, 79, 32, 114, 156 };

These are hardcoded in the class code, they are used to encrypt and desencrypt the text.

Everything is working fine, but i need to be able to manipulate these values, this is, i want to create on the user interface textboxes that can insert values like those ones, and the algorithm gets these values and works with them.

if i read these arrays as string i get:

7B-D9-13-0B-18-1A-55-2D-72-B8-1B-A2-25-70-DE-D1-F1-18-AF-90-AD-35-C4-1D-18-1A-11-DA-83-EC-35-D1

All i need is, read a string like "123, 217, 19, 11, 24, 26, 85, 45", and transform this in byte array like the ones i already have, so i can use the cryptography class with different byte arrays. And we can skip the part of diving the string by the "," and removing the blank spaces.

Ive researched but i just cant find anything, is all talking about streaming, i need to work with them like a normal string or int array, conversions, array index, etc...

Any ideas?

3
  • Just to clarify, your question is as simple as "How do I parse a string of comma separated bytes into a byte array?" Commented Nov 2, 2013 at 19:28
  • i want to pass the string, "123, 217, 19, 11, 24, 26, 85, 45", and convert it to to byte array like the ones i already have, yes, Key[0] = 123, Key[1] = 217, etc.., i tried to do this in the usual way and it didn't worked Commented Nov 2, 2013 at 19:31
  • why, is that simple? lol Commented Nov 2, 2013 at 19:32

1 Answer 1

1

You can parse a string of comma separated bytes into a byte array like so:

public static byte[] ParseByteArrayFromInput(string input)
{
    return input.Split(',').Select(s => byte.Parse(s.Trim())).ToArray();
}

It would expect an input like "1, 2, 3, 4". It doesn't do any "validation" to ensure it can actually be parsed. That's another exercise. If the input were something like "Cat, Dog", it would fail. You'll need to make sure you have a using System.Linq; at the top of your source file.

Here's how it works: First it takes the input and splits it into a string array with a comma as a separator. Then it trims away any whitespace, calls byte.Parse, then converts it all into an array. Here is the code broken down with some comments:

public static byte[] ParseByteArrayFromInput(string input)
{
    var splitInput = input.Split(','); //Splits the input into a string array. The "split" happens on the comma character.
    var convertToBytes = splitInput.Select(s => //for each of these strings in the split input...
    {
        var trim = s.Trim(); //Trim away any whitespace surrounding the number.
        return byte.Parse(trim); //Parse the trimmed string into a byte.
    });
    return convertToBytes.ToArray();//Convert it into an array.
}

This is an "expanded" version of the previous, just with comments and a little different syntax. Take a look at the MSDN documentation on LINQ to learn more about LINQ, too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.