1

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.

2
  • 1
    What are you trying to achieve? What is the code supposed to help with? Commented Oct 14, 2011 at 16:57
  • This code basically takes in input from a textbox, and on keypress, in 3 output textbox's will show an ascii text string converted into hex, decimal and reverse(not displayed in above code). The sum functionality I'm trying to build is to take the array of generated hex/decimal characters for a particular ascii string, and show it's calculated sum'd value (for statistical information). Commented Oct 14, 2011 at 17:01

1 Answer 1

5

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")));
Sign up to request clarification or add additional context in comments.

3 Comments

Christopher, that's amazing. I do however have one request, how would I use this Linq magic to get the sum of the Hex returned value as well? Thanks again by the way.
You want the sum in a hex value as well? x.Sum(x => x).ToString("X"); For ints (and some other data types) you can pass arguments into the ToString() function and it will format it for you. Link
Very informative, thank you again Christopher, you're my hero.

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.