-1
string hash = "4A|DA|6C|A9|C2|D5|71|EF|6E|2A|8C|C3|C9|4D|36|B9"
splitRHash2 = splitRHash.Split('|');
foreach (string i in splitRHash2)
{
    //BYTEARRAY += Convert.ToByte(Convert.ToInt32(i, 16))???
}

I have no idea of going about this. I simply wanted this string of hex:

4ADA6CA9C2D571EF6E2A8CC3C94D36B9

Into a byte array with 16 bytes. This will greatly help me to call these values from the 'hash' and use it add round keys later on for a project. The problem is, I have no knowledge in getting the string at increments of 2 without using the .split method. Any ideas? Thanks!!

Alex
  • 35
  • 5

3 Answers3

1

Simply use LINQ to convert the splitted strings to bytes and to an array afterwards. Here is the code:

string hash = "4A|DA|6C|A9|C2|D5|71|EF|6E|2A|8C|C3|C9|4D|36|B9";
string[] splittedHash = hash.Split('|');
byte[] byteHash = splittedHash.Select(b => Convert.ToByte(b, 16)).ToArray();
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • Thank you this is amazing! I really don't care for absolutely avoiding the .split(), in this case, its a pretty clean way of going about it. – Alex Nov 25 '17 at 18:38
0

You talking about something like this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class Program
    {
        static void Main (string[] args)
        {
            var str = "4ADA6CA9C2D571EF6E2A8CC3C94D36B9";

            var result = Partition (str, 2).ToArray ();
        }

        public static IEnumerable<string> Partition (string str, int partSize)
        {
            if (str == null) throw new ArgumentNullException ();
            if (partSize < 1) throw new ArgumentOutOfRangeException ();

            var sb = new StringBuilder (partSize);

            for (int i = 0; i < str.Length; i++) 
            {
                sb.Append (str[i]);

                bool isLastChar = i == str.Length - 1;

                if (sb.Length == partSize || isLastChar)
                {
                    yield return sb.ToString ();
                    sb.Clear ();
                }
            }
        }
    }
}
apocalypse
  • 5,764
  • 9
  • 47
  • 95
0

You could make the solution with only basic data structures and O(n) time like this.

string hash = "4A|DA|6C|A9|C2|D5|71|EF|6E|2A|8C|C3|C9|4D|36|B9";
byte[] result = new byte[16];

int i = 0;
int j = 0;
while(i < hash.Length)
{
    byte value = (byte)(HexCharToByte(hash[i]) * 16 + HexCharToByte(hash[i + 1]));
    result[j] = value;
    i += 3;
    j++;
}

For HexCharToByte(), you could make up something like this:

static byte HexCharToByte(char c)
{
    HashSet<char> NumSet = new HashSet<char>( new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'} );
    HashSet<char> CharSet = new HashSet<char>( new char[] { 'A', 'B', 'C', 'D', 'E', 'F' } );

    if (NumSet.Contains(c))
    {
        return (byte)(c - '0');
    }
    else if (CharSet.Contains(c))
    {
        return (byte)(c - 'A' + 10);
    }

    throw new InvalidArgumentException("c");
}
NonStatic
  • 951
  • 1
  • 8
  • 27