Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:52:18+00:00 2026-05-26T08:52:18+00:00

So I have these two functions, public static string[] CharToHex(string str, string prefix, string

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:52:19+00:00Added an answer on May 26, 2026 at 8:52 am

    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")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
I have MethodBases for two functions: public static int Add(params int[] parameters) { /*
I have these two functions (with Point2D & LineVector (has 2 Point2D member variables)
I have these two pieces of code, wich one is more readable? foreach decimal
I have these two modules : package G1; sub new { my $class =
I have these two classes public class Person { } public class Company {
I have these two situations: String s = aa; s = s + aa;
I have build a custom Html Helper extension as follows: public static string DatePicker(this
OK I have a static class that has two static members, a string and
I have two classes named ROAD and PATH public class ROAD { public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.