-3

I have an String, which looks like:

string a = "1AD9B1E7D11FEA4F4C89493E1";

and now i want to split this string after every 4th character, so that it looks like this after spliting:

string[] splitted = { "1AD9", "B1E7", "D11F", "EA4F", "C894", "93E1" };

then i want to convert each of these strings to an int, so i basicly need to convert hexadecimal to int(i think) and then i want to do some math with these ints and then at the end convert it back to "string" or hexadecimal and then put the different strings together, so that i have a string like at the beginning. thanks in advance :)

N.W.A
  • 65
  • 9
  • 5
    You should show what have you tried so far. Otherwise your question belongs to the category 'give me teh codez' and should be closed – Steve Nov 03 '15 at 22:22
  • Please include that in the question by editing your post... – aschipfl Nov 03 '15 at 22:25
  • Possible duplicate of [C# convert integer to hex and back again](http://stackoverflow.com/questions/1139957/c-sharp-convert-integer-to-hex-and-back-again) – Fabian Winkler Nov 04 '15 at 00:07
  • @Steve and yet, how many will line up to give it? Boo, putresence and filth. – Marc L. Nov 04 '15 at 04:43

3 Answers3

0

To convert to the string array:

string a = "1AD9B1E7D11FEA4F4C89493E1";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < a.Length; i++)
{
    if (i % 4 == 0 && i != 0)
        sb.Append(' ');
    sb.Append(a[i]);
}
string formatted = sb.ToString();
string[] splitted = formatted.Split(' ');

The above just inserts a space every 4th character, then splits the string at the spaces

To convert to int:

foreach(string s in splitted){
    int num = Int32.Parse(s, System.Globalization.NumberStyles.HexNumber);
    //do what you want with num
}

To convert back to hex, you can use:

string someString = someInteger.ToString("X4");

The x4 just means the string will be 4 digits long

Here is a working example that just prints the converted int and converted hex values:

            string a = "1AD9B1E7D11FEA4F4C89493E1";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < a.Length; i++)
            {
                if (i % 4 == 0 && i != 0)
                    sb.Append(' ');
                sb.Append(a[i]);
            }
            string formatted = sb.ToString();
            string[] splitted = formatted.Split(' ');

            foreach(string s in splitted){
                int num = Int32.Parse(s, System.Globalization.NumberStyles.HexNumber);
                Console.WriteLine(num);
                string someString = num.ToString("X4");
                Console.WriteLine(someString);
            }
Blue Boy
  • 600
  • 1
  • 5
  • 12
  • okay the converting from an int to hex works thanks :) but the conversion from the splitted string to int crashes :( – N.W.A Nov 03 '15 at 22:34
  • I've edited my answer, just realised I didn't cover spliiting the word into an array – Blue Boy Nov 03 '15 at 22:48
  • Editted again, changed the for loop to a foreach loop, that should work now :) – Blue Boy Nov 03 '15 at 22:51
  • okay, the for loop gives an error, it says insert ";", but i don't know where, i used to use forloops like For(int i = 0;i<3;i++) :D sry for being a noob, i just started getting into the c-language – N.W.A Nov 03 '15 at 22:53
  • Which for loop creates the error? Did you see my edit about changing it to a foreach loop? – Blue Boy Nov 03 '15 at 22:56
  • I've added a working example – Blue Boy Nov 03 '15 at 22:59
  • thanks, that works perfectly, as far as i can see you are pretty good at the c-language, so you maybe also know how to do the following, and avoid me from posting again ^^ I have an inputstring, and i want to check if this string fits the format of a date: it should return true if the string looks like "00.00.0000 * 00.00" and false if it doesnt looks like that, i didn't knew how to set that the 00 can be numbers from 0-9 because making a comparison to every single possible date would take ways to long. – N.W.A Nov 03 '15 at 23:09
  • No problem, could you possibly set my answer to correct? Honestly I think you should post that as a separate question as it could help others with the same problem – Blue Boy Nov 03 '15 at 23:12
0

The first thing is splitting the string. This can be accomplished easily using a for loop and Skip() and Take(). I went with this method because if your string length isn't a multiple of 4 Substring() will not catch any end characters. Also, I used a List<string> for the split as I don't know if the input string is a fixed length or not. If it is a fixed length, you could easily replace it with a string[].

var splitted = new List<string>();

for(int i = 0; i < a.Length; i += 4)
{
    var s = new string(a.Skip(i).Take(4).ToArray());
    splitted.Add(s);
}

Then you can parse your numbers into integers and do your calculations on each number and store them into a new List<int>

var numbers = new List<int>();

foreach (var item in splitted)
{
    var n = int.Parse(item, System.Globalization.NumberStyles.HexNumber);

    int result = // Do whatever you need to do to each number.

    numbers.Add(result);
}

After that, you can just loop through the numbers and use a StringBuilder to rebuild the string.

StringBuilder sb = new StringBuilder();

foreach(var item in numbers)
{
    sb.Append(item.ToString("X"));
}

Then just call

sb.ToString();

to get your resultant string.

If you need the numbers directly to do math, you can just store the numbers in the numbers list instead and do your calculations as you want. Then you can loop through the results of those calculations in the final foreach loop instead to get your rebuilt string.

Bradford Dillon
  • 1,610
  • 10
  • 23
0

Might as well get my two sense into this question

            string hex = "0123456789ABCDEF";
            string a = "1AD9B1E7D11FEA4FC89493E1";
            int[] nibbles = a.Select(x => hex.IndexOf(x)).ToArray();
            List<UInt16> results = new List<ushort>();
            for (int i = 0; i < nibbles.Count(); i += 4)
            {
                results.Add((ushort)((nibbles[i] << 12) | (nibbles[i + 1] << 8) | (nibbles[i + 2] << 4) | nibbles[i + 3]) );
            }​
            string[] test = results.Select(x => x.ToString("X4")).ToArray();​
jdweng
  • 28,546
  • 2
  • 13
  • 18