0

I'm looking for a C# code to split a very large string (hex values) into 8-size chunks (array of strings) so that I could turn them into ints one by one.

The hex value string looks like ...000000030000000800000002... and I want them in 00000003, 00000008, 00000002, etc.

Much appreciated!

theGlitch
  • 43
  • 2
  • 7
  • Note that if your goal to get list of `int` and not split the string you'd better use approaches suggested in [How do you convert Byte Array to Hexadecimal String](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa?rq=1) to avoid creation (even lazy creation as in duplicate question) of the intermediate string altogether. – Alexei Levenkov Feb 07 '14 at 17:18

3 Answers3

1

this is a easy to understand version:

string x = "0000000100000002000000003";

List<string> a = new List<string>();
for (int i = 0; i < x.Length; i += 8)
{
    if((i + 8) < x.Length)
        a.Add(x.Substring(i, 8));
    else
        a.Add(x.Substring(i));
}

result is:

a[0] = 00000001;
a[1] = 00000002;
a[2] = 00000003;
Only a Curious Mind
  • 2,807
  • 23
  • 39
0

I think this maybe what you're looking for:

string sentence = "0000000100000002000000003";
string[] digits = Regex.Split(sentence, ".{8}");

Result:

digits[0] = "00000001"
digits[1] = "00000002"
digits[2] = "00000003"
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • +0: Using regex + creating a lot of strings at the same time... You need to add explanation of regex you are using - don't assuming that everyone knows its syntax. – Alexei Levenkov Feb 07 '14 at 17:16
  • @AlexeiLevenkov, Usually i will agree with you, and almost always will favor the iterative way rather the recursive or in this case Regular expression way. I still publish this scenario because someone else already publish iterative `Substring` scenario, so i used this stage to show another implementation for this scenario. – Orel Eraki Feb 07 '14 at 19:01
0

You can use the String.Split method on your string like so:

string longString = "10101010,10101010,10101010"
string[] Array;
Array = longString.Split(",");

To get the comma separated string, you could use @Sameer's Regex idea.

vasilescur
  • 240
  • 2
  • 5
  • 16