Clu
Clu

Reputation: 339

c# Sum to string of Char array

So I have these two functions,

public static string[] CharToHex(string str, string prefix, string delimeter)
    {
        List<string> list = new List<string>();
        foreach (char c in str)
        {
            list.Add(prefix + String.Format("{0:X2}",(int)c) + delimeter);
        }
        return list.ToArray();
    }
public static string[] StrToChar(string str, string prefix, string delimeter)
    {
        List<string> list = new List<string>();
        foreach (char c in str)
        {
            list.Add(prefix + (int)c + delimeter);
        }
        return list.ToArray();
    }

Basically, I'm trying to show the Sum'd value of both returned arrays in a label.

I created a function to calculate a sum,

public static string ArraySum(int[] array)
    {
        string sum = array.Sum().ToString();
        return sum;
    }

And another function to take the string array and convert it to a string,

public static string StringArrayToString(string[] array)
    {
        StringBuilder builder = new StringBuilder();
        foreach (string value in array)
        {
            builder.Append(value);
        }
        return builder.ToString();
    }

This is how I'm putting it all together,

        string[] dec = StrToChar(txtInput.Text, txtPrefix.Text, txtDelimiter.Text);
        string[] hex = CharToHex(txtInput.Text, txtPrefix.Text, txtDelimiter.Text);
        string decStr = StringArrayToString(dec);
        string hexStr = StringArrayToString(hex);
        int[] decCount = dec.Select(x => int.Parse(x)).ToArray();
        int[] hexCount = hex.Select(x => int.Parse(x)).ToArray();

        var builder = new StringBuilder();
        Array.ForEach(decCount, x => builder.Append(x));
        var res = builder.ToString();

        txtDecimal.Text = decStr;
        txtHex.Text = hexStr;
        lblDecimalSum.Text = res;

The issue here is, this obviously isn't working, it also seems horribly inefficient, there has to be an easier way of doing all of this and also, my sum isn't properly summing up the array elements.

I'm not entirely sure how to go about doing this and any assistance / feedback would be greatly appreciated.

Thank you kindly.

Upvotes: 1

Views: 10374

Answers (1)

Christopher Currens
Christopher Currens

Reputation: 30695

If I understand you correctly, you're trying to get the add the value of each character of a string together, not parse an int from a string and add those together. If that's the case, you can do it with linq:

string x = "xasdgdfhdsfh";
int sum = x.Sum(b => b);

In fact using linq, you can accomplish everything you want to do:

string x = "xasdgdfhdsfh";
string delim = txtDelimiter.Text;
string prefix = txtPrefix.Text;

lblDecimalSum.Text = x.Sum(c => c).ToString();

txtDecimal.Text = 
            string.Join(delim, x.Select(c => prefix + ((int)c).ToString()));

txtHex.Text = 
            string.Join(delim, x.Select(c => prefix + ((int)c).ToString("X2")));

Upvotes: 5

Related Questions